Data Cleaning & Manipulation

Monday, April 17

Today we will…

Example Data set – Cereal

library(liver)
data(cereal)
str(cereal, give.attr = FALSE)
'data.frame':   77 obs. of  16 variables:
 $ name    : Factor w/ 77 levels "100% Bran","100% Natural Bran",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ manuf   : Factor w/ 7 levels "A","G","K","N",..: 4 6 3 3 7 2 3 2 7 5 ...
 $ type    : Factor w/ 2 levels "cold","hot": 1 1 1 1 1 1 1 1 1 1 ...
 $ calories: int  70 120 70 50 110 110 110 130 90 90 ...
 $ protein : int  4 3 4 4 2 2 2 3 2 3 ...
 $ fat     : int  1 5 1 0 2 2 0 2 1 0 ...
 $ sodium  : int  130 15 260 140 200 180 125 210 200 210 ...
 $ fiber   : num  10 2 9 14 1 1.5 1 2 4 5 ...
 $ carbo   : num  5 8 7 8 14 10.5 11 18 15 13 ...
 $ sugars  : int  6 8 5 0 8 10 14 8 6 5 ...
 $ potass  : int  280 135 320 330 -1 70 30 100 125 190 ...
 $ vitamins: int  25 0 25 25 25 25 25 25 25 25 ...
 $ shelf   : int  3 3 3 3 3 1 2 3 1 3 ...
 $ weight  : num  1 1 1 1 1 1 1 1.33 1 1 ...
 $ cups    : num  0.33 1 0.33 0.5 0.75 0.75 1 0.75 0.67 0.67 ...
 $ rating  : num  68.4 34 59.4 93.7 34.4 ...
head(cereal)
                       name manuf type calories protein fat sodium fiber carbo
1                 100% Bran     N cold       70       4   1    130  10.0   5.0
2         100% Natural Bran     Q cold      120       3   5     15   2.0   8.0
3                  All-Bran     K cold       70       4   1    260   9.0   7.0
4 All-Bran with Extra Fiber     K cold       50       4   0    140  14.0   8.0
5            Almond Delight     R cold      110       2   2    200   1.0  14.0
6   Apple Cinnamon Cheerios     G cold      110       2   2    180   1.5  10.5
  sugars potass vitamins shelf weight cups   rating
1      6    280       25     3      1 0.33 68.40297
2      8    135        0     3      1 1.00 33.98368
3      5    320       25     3      1 0.33 59.42551
4      0    330       25     3      1 0.50 93.70491
5      8     -1       25     3      1 0.75 34.38484
6     10     70       25     1      1 0.75 29.50954
summary(cereal)
                        name    manuf    type       calories    
 100% Bran                : 1   A: 1   cold:74   Min.   : 50.0  
 100% Natural Bran        : 1   G:22   hot : 3   1st Qu.:100.0  
 All-Bran                 : 1   K:23             Median :110.0  
 All-Bran with Extra Fiber: 1   N: 6             Mean   :106.9  
 Almond Delight           : 1   P: 9             3rd Qu.:110.0  
 Apple Cinnamon Cheerios  : 1   Q: 8             Max.   :160.0  
 (Other)                  :71   R: 8                            
    protein           fat            sodium          fiber       
 Min.   :1.000   Min.   :0.000   Min.   :  0.0   Min.   : 0.000  
 1st Qu.:2.000   1st Qu.:0.000   1st Qu.:130.0   1st Qu.: 1.000  
 Median :3.000   Median :1.000   Median :180.0   Median : 2.000  
 Mean   :2.545   Mean   :1.013   Mean   :159.7   Mean   : 2.152  
 3rd Qu.:3.000   3rd Qu.:2.000   3rd Qu.:210.0   3rd Qu.: 3.000  
 Max.   :6.000   Max.   :5.000   Max.   :320.0   Max.   :14.000  
                                                                 
     carbo          sugars           potass          vitamins     
 Min.   :-1.0   Min.   :-1.000   Min.   : -1.00   Min.   :  0.00  
 1st Qu.:12.0   1st Qu.: 3.000   1st Qu.: 40.00   1st Qu.: 25.00  
 Median :14.0   Median : 7.000   Median : 90.00   Median : 25.00  
 Mean   :14.6   Mean   : 6.922   Mean   : 96.08   Mean   : 28.25  
 3rd Qu.:17.0   3rd Qu.:11.000   3rd Qu.:120.00   3rd Qu.: 25.00  
 Max.   :23.0   Max.   :15.000   Max.   :330.00   Max.   :100.00  
                                                                  
     shelf           weight          cups           rating     
 Min.   :1.000   Min.   :0.50   Min.   :0.250   Min.   :18.04  
 1st Qu.:1.000   1st Qu.:1.00   1st Qu.:0.670   1st Qu.:33.17  
 Median :2.000   Median :1.00   Median :0.750   Median :40.40  
 Mean   :2.208   Mean   :1.03   Mean   :0.821   Mean   :42.67  
 3rd Qu.:3.000   3rd Qu.:1.00   3rd Qu.:1.000   3rd Qu.:50.83  
 Max.   :3.000   Max.   :1.50   Max.   :1.500   Max.   :93.70  
                                                               

Data Wrangling with dplyr

dplyr

dplyr provides us with the “Grammar of Data Manipulation”.

  • This package gives us the tools to wrangle, manipulate, and tidy our data with ease.
  • Check out the dplyr cheatsheet.

Data wrangling by Allison Horst

dplyr verbs

  • filter()select rows based on their values
  • arrange()sort rows based on their values
  • select()select columns
  • mutate()add new columns by transforming other columns
  • summarize() – perform summary operations on columns
  • group_by() – facilitate group-wise operations
  • Use the pipe operator (|> or %>%) to chain together data wrangling operations.

The Pipe Operator

No matter how complex and polished the individual operations are, it is often the quality of the glue that most directly determines the power of the system.

— Hal Abelson

The Pipe Operator

  • With dplyr, your code should read like a sentence.
  • The pipe operator is an important part of that readability.

  • The data is the primary object in your sentence, so it should come first in your code.

The Pipe Operator

  • The pipe specifies a sequence of operations.
  • The output from one operation is passed into the first argument of the next operation.
  • The original pipe: %>%

    • Loaded with tidyverse package (part of magrittr).
  • The “native” pipe: |>

    • Was created in R version 4.1.0.
    • Tools > Global Options... > Code > check Use native pipe operator.

The Pipe Operator

dr_rehnberg |>
  do_a_dance()


dr_rehnberg |>
  put_on("cool hat") |>
  do_a_dance(type = "macarena")

Data Comes First!

  • filter(data = cereal, ...)
  • select(data = cereal, ...)
  • mutate(data = cereal, ...)

These are equivalent:

summary(data = cereal)
cereal |> 
  summary()


The pipe operator is your friend!

You can also pipe manipulated data or summaries directly into your ggplot2 code for plotting!

filter()

dplyr filter() by Allison Horst

filter()

We filter to the rows (observations) we would like to keep in the data.

cereal |> 
  filter(calories <= 100)
name manuf type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
100% Bran N cold 70 4 1 130 10.0 5.0 6 280 25 3 1.00 0.33 68.40297
All-Bran K cold 70 4 1 260 9.0 7.0 5 320 25 3 1.00 0.33 59.42551
All-Bran with Extra Fiber K cold 50 4 0 140 14.0 8.0 0 330 25 3 1.00 0.50 93.70491
Bran Chex R cold 90 2 1 200 4.0 15.0 6 125 25 1 1.00 0.67 49.12025
Bran Flakes P cold 90 3 0 210 5.0 13.0 5 190 25 3 1.00 0.67 53.31381
Corn Flakes K cold 100 2 0 290 1.0 21.0 2 35 25 1 1.00 1.00 45.86332
Cream of Wheat (Quick) N hot 100 3 0 80 1.0 21.0 0 -1 0 2 1.00 1.00 64.53382
Crispy Wheat & Raisins G cold 100 2 1 140 2.0 11.0 10 120 25 3 1.00 0.75 36.17620
Double Chex R cold 100 2 0 190 1.0 18.0 5 80 25 3 1.00 0.75 44.33086
Frosted Mini-Wheats K cold 100 3 0 0 3.0 14.0 7 100 25 2 1.00 0.80 58.34514
Golden Crisp P cold 100 2 0 45 0.0 11.0 15 40 25 1 1.00 0.88 35.25244
Grape Nuts Flakes P cold 100 3 1 140 3.0 15.0 5 85 25 3 1.00 0.88 52.07690
Life Q cold 100 4 2 150 2.0 12.0 6 95 25 2 1.00 0.67 45.32807
Maypo A hot 100 4 1 0 0.0 16.0 3 95 25 2 1.00 1.00 54.85092
Multi-Grain Cheerios G cold 100 2 1 220 2.0 15.0 6 90 25 1 1.00 1.00 40.10596
Nutri-grain Wheat K cold 90 3 0 170 3.0 18.0 2 90 25 3 1.00 1.00 59.64284
Product 19 K cold 100 3 0 320 1.0 20.0 3 45 100 3 1.00 1.00 41.50354
Puffed Rice Q cold 50 1 0 0 0.0 13.0 0 15 0 3 0.50 1.00 60.75611
Puffed Wheat Q cold 50 2 0 0 1.0 10.0 0 50 0 3 0.50 1.00 63.00565
Quaker Oat Squares Q cold 100 4 1 135 2.0 14.0 6 110 25 3 1.00 0.50 49.51187
Quaker Oatmeal Q hot 100 5 2 0 2.7 -1.0 -1 110 0 1 1.00 0.67 50.82839
Raisin Nut Bran G cold 100 3 2 140 2.5 10.5 8 140 25 3 1.00 0.50 39.70340
Raisin Squares K cold 90 2 0 0 2.0 15.0 6 110 25 3 1.00 0.50 55.33314
Shredded Wheat N cold 80 2 0 0 3.0 16.0 0 95 0 1 0.83 1.00 68.23588
Shredded Wheat 'n'Bran N cold 90 3 0 0 4.0 19.0 0 140 0 1 1.00 0.67 74.47295
Shredded Wheat spoon size N cold 90 3 0 0 3.0 20.0 0 120 0 1 1.00 0.67 72.80179
Strawberry Fruit Wheats N cold 90 2 0 15 3.0 15.0 5 90 25 2 1.00 1.00 59.36399
Total Whole Grain G cold 100 3 1 200 3.0 16.0 3 110 100 3 1.00 1.00 46.65884
Wheat Chex R cold 100 3 1 230 3.0 17.0 3 115 25 1 1.00 0.67 49.78744
Wheaties G cold 100 3 1 200 3.0 17.0 3 110 25 1 1.00 1.00 51.59219

filter()

We can add multiple filters to our data, to get a more specific subset.

cereal |> 
  filter(calories <= 100,
         type == "hot")
name manuf type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
Cream of Wheat (Quick) N hot 100 3 0 80 1.0 21 0 -1 0 2 1 1.00 64.53382
Maypo A hot 100 4 1 0 0.0 16 3 95 25 2 1 1.00 54.85092
Quaker Oatmeal Q hot 100 5 2 0 2.7 -1 -1 110 0 1 1 0.67 50.82839

filter(): Handy Helpers!

  • > greater than
  • < less than
  • == equal to
  • ! not
  • %in% identifies if an element belongs to a vector
  • & / , and
  • | or
  • is.na() binary evaluation of missing values

filter(): |

cereal |> 
  filter(calories <= 100,
         type == "hot")
name manuf type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
Cream of Wheat (Quick) N hot 100 3 0 80 1.0 21 0 -1 0 2 1 1.00 64.53382
Maypo A hot 100 4 1 0 0.0 16 3 95 25 2 1 1.00 54.85092
Quaker Oatmeal Q hot 100 5 2 0 2.7 -1 -1 110 0 1 1 0.67 50.82839

What if I wanted either “low calorie” cereals or hot cereals…

Code
cereal |> 
  filter(calories <= 100 |
           type == "hot")

filter(): %in%

Are you interested in observations with values in a list of levels?

cereal |> 
  filter(name %in% c("Cheerios", "Cinnamon Toast Crunch", "Raisin Bran", "Cracklin' Oat Bran"))
name manuf type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
Cheerios G cold 110 6 2 290 2 17 1 105 25 1 1.00 1.25 50.76500
Cinnamon Toast Crunch G cold 120 1 3 210 0 13 9 45 25 2 1.00 0.75 19.82357
Cracklin' Oat Bran K cold 110 3 3 140 4 10 7 160 25 3 1.00 0.50 40.44877
Raisin Bran K cold 120 3 1 210 5 14 12 240 25 2 1.33 0.75 39.25920

How do we “filter” in base R?

You can use the subset() function!

subset(cereal, name %in% c("Cheerios", "Cinnamon Toast Crunch", "Raisin Bran", "Cracklin' Oat Bran"))
name manuf type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
12 Cheerios G cold 110 6 2 290 2 17 1 105 25 1 1.00 1.25 50.76500
13 Cinnamon Toast Crunch G cold 120 1 3 210 0 13 9 45 25 2 1.00 0.75 19.82357
20 Cracklin' Oat Bran K cold 110 3 3 140 4 10 7 160 25 3 1.00 0.50 40.44877
59 Raisin Bran K cold 120 3 1 210 5 14 12 240 25 2 1.33 0.75 39.25920
subset(cereal, calories <= 100 & type == "hot")
name manuf type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
21 Cream of Wheat (Quick) N hot 100 3 0 80 1.0 21 0 -1 0 2 1 1.00 64.53382
44 Maypo A hot 100 4 1 0 0.0 16 3 95 25 2 1 1.00 54.85092
58 Quaker Oatmeal Q hot 100 5 2 0 2.7 -1 -1 110 0 1 1 0.67 50.82839

arrange()

arrange()

We arrange the rows of the data in order of a particular variable.


cereal |> 
  arrange(calories)
name manuf type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
All-Bran with Extra Fiber K cold 50 4 0 140 14.0 8.0 0 330 25 3 1.00 0.50 93.70491
Puffed Rice Q cold 50 1 0 0 0.0 13.0 0 15 0 3 0.50 1.00 60.75611
Puffed Wheat Q cold 50 2 0 0 1.0 10.0 0 50 0 3 0.50 1.00 63.00565
100% Bran N cold 70 4 1 130 10.0 5.0 6 280 25 3 1.00 0.33 68.40297
All-Bran K cold 70 4 1 260 9.0 7.0 5 320 25 3 1.00 0.33 59.42551
Shredded Wheat N cold 80 2 0 0 3.0 16.0 0 95 0 1 0.83 1.00 68.23588
Bran Chex R cold 90 2 1 200 4.0 15.0 6 125 25 1 1.00 0.67 49.12025
Bran Flakes P cold 90 3 0 210 5.0 13.0 5 190 25 3 1.00 0.67 53.31381
Nutri-grain Wheat K cold 90 3 0 170 3.0 18.0 2 90 25 3 1.00 1.00 59.64284
Raisin Squares K cold 90 2 0 0 2.0 15.0 6 110 25 3 1.00 0.50 55.33314
Shredded Wheat 'n'Bran N cold 90 3 0 0 4.0 19.0 0 140 0 1 1.00 0.67 74.47295
Shredded Wheat spoon size N cold 90 3 0 0 3.0 20.0 0 120 0 1 1.00 0.67 72.80179
Strawberry Fruit Wheats N cold 90 2 0 15 3.0 15.0 5 90 25 2 1.00 1.00 59.36399
Corn Flakes K cold 100 2 0 290 1.0 21.0 2 35 25 1 1.00 1.00 45.86332
Cream of Wheat (Quick) N hot 100 3 0 80 1.0 21.0 0 -1 0 2 1.00 1.00 64.53382
Crispy Wheat & Raisins G cold 100 2 1 140 2.0 11.0 10 120 25 3 1.00 0.75 36.17620
Double Chex R cold 100 2 0 190 1.0 18.0 5 80 25 3 1.00 0.75 44.33086
Frosted Mini-Wheats K cold 100 3 0 0 3.0 14.0 7 100 25 2 1.00 0.80 58.34514
Golden Crisp P cold 100 2 0 45 0.0 11.0 15 40 25 1 1.00 0.88 35.25244
Grape Nuts Flakes P cold 100 3 1 140 3.0 15.0 5 85 25 3 1.00 0.88 52.07690
Life Q cold 100 4 2 150 2.0 12.0 6 95 25 2 1.00 0.67 45.32807
Maypo A hot 100 4 1 0 0.0 16.0 3 95 25 2 1.00 1.00 54.85092
Multi-Grain Cheerios G cold 100 2 1 220 2.0 15.0 6 90 25 1 1.00 1.00 40.10596
Product 19 K cold 100 3 0 320 1.0 20.0 3 45 100 3 1.00 1.00 41.50354
Quaker Oat Squares Q cold 100 4 1 135 2.0 14.0 6 110 25 3 1.00 0.50 49.51187
Quaker Oatmeal Q hot 100 5 2 0 2.7 -1.0 -1 110 0 1 1.00 0.67 50.82839
Raisin Nut Bran G cold 100 3 2 140 2.5 10.5 8 140 25 3 1.00 0.50 39.70340
Total Whole Grain G cold 100 3 1 200 3.0 16.0 3 110 100 3 1.00 1.00 46.65884
Wheat Chex R cold 100 3 1 230 3.0 17.0 3 115 25 1 1.00 0.67 49.78744
Wheaties G cold 100 3 1 200 3.0 17.0 3 110 25 1 1.00 1.00 51.59219
Almond Delight R cold 110 2 2 200 1.0 14.0 8 -1 25 3 1.00 0.75 34.38484
Apple Cinnamon Cheerios G cold 110 2 2 180 1.5 10.5 10 70 25 1 1.00 0.75 29.50954
Apple Jacks K cold 110 2 0 125 1.0 11.0 14 30 25 2 1.00 1.00 33.17409
Cheerios G cold 110 6 2 290 2.0 17.0 1 105 25 1 1.00 1.25 50.76500
Clusters G cold 110 3 2 140 2.0 13.0 7 105 25 3 1.00 0.50 40.40021
Cocoa Puffs G cold 110 1 1 180 0.0 12.0 13 55 25 2 1.00 1.00 22.73645
Corn Chex R cold 110 2 0 280 0.0 22.0 3 25 25 1 1.00 1.00 41.44502
Corn Pops K cold 110 1 0 90 1.0 13.0 12 20 25 2 1.00 1.00 35.78279
Count Chocula G cold 110 1 1 180 0.0 12.0 13 65 25 2 1.00 1.00 22.39651
Cracklin' Oat Bran K cold 110 3 3 140 4.0 10.0 7 160 25 3 1.00 0.50 40.44877
Crispix K cold 110 2 0 220 1.0 21.0 3 30 25 3 1.00 1.00 46.89564
Froot Loops K cold 110 2 1 125 1.0 11.0 13 30 25 2 1.00 1.00 32.20758
Frosted Flakes K cold 110 1 0 200 1.0 14.0 11 25 25 1 1.00 0.75 31.43597
Fruity Pebbles P cold 110 1 1 135 0.0 13.0 12 25 25 2 1.00 0.75 28.02576
Golden Grahams G cold 110 1 1 280 0.0 15.0 9 45 25 2 1.00 0.75 23.80404
Grape-Nuts P cold 110 3 0 170 3.0 17.0 3 90 25 3 1.00 0.25 53.37101
Honey Nut Cheerios G cold 110 3 1 250 1.5 11.5 10 90 25 1 1.00 0.75 31.07222
Honey-comb P cold 110 1 0 180 0.0 14.0 11 35 25 1 1.00 1.33 28.74241
Just Right Crunchy Nuggets K cold 110 2 1 170 1.0 17.0 6 60 100 3 1.00 1.00 36.52368
Kix G cold 110 2 1 260 0.0 21.0 3 40 25 2 1.00 1.50 39.24111
Lucky Charms G cold 110 2 1 180 0.0 12.0 12 55 25 2 1.00 1.00 26.73451
Rice Chex R cold 110 1 0 240 0.0 23.0 2 30 25 1 1.00 1.13 41.99893
Rice Krispies K cold 110 2 0 290 0.0 22.0 3 35 25 1 1.00 1.00 40.56016
Smacks K cold 110 2 1 70 1.0 9.0 15 40 25 2 1.00 0.75 31.23005
Special K K cold 110 6 0 230 1.0 16.0 3 55 25 1 1.00 1.00 53.13132
Total Corn Flakes G cold 110 2 1 200 0.0 21.0 3 35 100 3 1.00 1.00 38.83975
Triples G cold 110 2 1 250 0.0 21.0 3 60 25 3 1.00 0.75 39.10617
Trix G cold 110 1 1 140 0.0 13.0 12 25 25 2 1.00 1.00 27.75330
Wheaties Honey Gold G cold 110 2 1 200 1.0 16.0 8 60 25 1 1.00 0.75 36.18756
100% Natural Bran Q cold 120 3 5 15 2.0 8.0 8 135 0 3 1.00 1.00 33.98368
Cap'n'Crunch Q cold 120 1 2 220 0.0 12.0 12 35 25 2 1.00 0.75 18.04285
Cinnamon Toast Crunch G cold 120 1 3 210 0.0 13.0 9 45 25 2 1.00 0.75 19.82357
Fruit & Fibre Dates; Walnuts; and Oats P cold 120 3 2 160 5.0 12.0 10 200 25 3 1.25 0.67 40.91705
Fruitful Bran K cold 120 3 0 240 5.0 14.0 12 190 25 3 1.33 0.67 41.01549
Great Grains Pecan P cold 120 3 3 75 3.0 13.0 4 100 25 3 1.00 0.33 45.81172
Honey Graham Ohs Q cold 120 1 2 220 1.0 12.0 11 45 25 2 1.00 1.00 21.87129
Nut&Honey Crunch K cold 120 2 1 190 0.0 15.0 9 40 25 2 1.00 0.67 29.92429
Post Nat. Raisin Bran P cold 120 3 1 200 6.0 11.0 14 260 25 3 1.33 0.67 37.84059
Raisin Bran K cold 120 3 1 210 5.0 14.0 12 240 25 2 1.33 0.75 39.25920
Basic 4 G cold 130 3 2 210 2.0 18.0 8 100 25 3 1.33 0.75 37.03856
Oatmeal Raisin Crisp G cold 130 3 2 170 1.5 13.5 10 120 25 3 1.25 0.50 30.45084
Just Right Fruit & Nut K cold 140 3 1 170 2.0 20.0 9 95 100 3 1.30 0.75 36.47151
Nutri-Grain Almond-Raisin K cold 140 3 2 220 3.0 21.0 7 130 25 3 1.33 0.67 40.69232
Total Raisin Bran G cold 140 3 1 190 4.0 15.0 14 230 100 3 1.50 1.00 28.59278
Muesli Raisins; Dates; & Almonds R cold 150 4 3 95 3.0 16.0 11 170 25 3 1.00 1.00 37.13686
Muesli Raisins; Peaches; & Pecans R cold 150 4 3 150 3.0 16.0 11 170 25 3 1.00 1.00 34.13976
Mueslix Crispy Blend K cold 160 3 2 150 3.0 17.0 13 160 25 3 1.50 0.67 30.31335

arrange()

We can arrange by multiple variables.


cereal |> 
  arrange(calories, sugars) |>
  select(c(1:4, 10))
name manuf type calories sugars
All-Bran with Extra Fiber K cold 50 0
Puffed Rice Q cold 50 0
Puffed Wheat Q cold 50 0
All-Bran K cold 70 5
100% Bran N cold 70 6
Shredded Wheat N cold 80 0
Shredded Wheat 'n'Bran N cold 90 0
Shredded Wheat spoon size N cold 90 0
Nutri-grain Wheat K cold 90 2
Bran Flakes P cold 90 5
Strawberry Fruit Wheats N cold 90 5
Bran Chex R cold 90 6
Raisin Squares K cold 90 6
Quaker Oatmeal Q hot 100 -1
Cream of Wheat (Quick) N hot 100 0
Corn Flakes K cold 100 2
Maypo A hot 100 3
Product 19 K cold 100 3
Total Whole Grain G cold 100 3
Wheat Chex R cold 100 3
Wheaties G cold 100 3
Double Chex R cold 100 5
Grape Nuts Flakes P cold 100 5
Life Q cold 100 6
Multi-Grain Cheerios G cold 100 6
Quaker Oat Squares Q cold 100 6
Frosted Mini-Wheats K cold 100 7
Raisin Nut Bran G cold 100 8
Crispy Wheat & Raisins G cold 100 10
Golden Crisp P cold 100 15
Cheerios G cold 110 1
Rice Chex R cold 110 2
Corn Chex R cold 110 3
Crispix K cold 110 3
Grape-Nuts P cold 110 3
Kix G cold 110 3
Rice Krispies K cold 110 3
Special K K cold 110 3
Total Corn Flakes G cold 110 3
Triples G cold 110 3
Just Right Crunchy Nuggets K cold 110 6
Clusters G cold 110 7
Cracklin' Oat Bran K cold 110 7
Almond Delight R cold 110 8
Wheaties Honey Gold G cold 110 8
Golden Grahams G cold 110 9
Apple Cinnamon Cheerios G cold 110 10
Honey Nut Cheerios G cold 110 10
Frosted Flakes K cold 110 11
Honey-comb P cold 110 11
Corn Pops K cold 110 12
Fruity Pebbles P cold 110 12
Lucky Charms G cold 110 12
Trix G cold 110 12
Cocoa Puffs G cold 110 13
Count Chocula G cold 110 13
Froot Loops K cold 110 13
Apple Jacks K cold 110 14
Smacks K cold 110 15
Great Grains Pecan P cold 120 4
100% Natural Bran Q cold 120 8
Cinnamon Toast Crunch G cold 120 9
Nut&Honey Crunch K cold 120 9
Fruit & Fibre Dates; Walnuts; and Oats P cold 120 10
Honey Graham Ohs Q cold 120 11
Cap'n'Crunch Q cold 120 12
Fruitful Bran K cold 120 12
Raisin Bran K cold 120 12
Post Nat. Raisin Bran P cold 120 14
Basic 4 G cold 130 8
Oatmeal Raisin Crisp G cold 130 10
Nutri-Grain Almond-Raisin K cold 140 7
Just Right Fruit & Nut K cold 140 9
Total Raisin Bran G cold 140 14
Muesli Raisins; Dates; & Almonds R cold 150 11
Muesli Raisins; Peaches; & Pecans R cold 150 11
Mueslix Crispy Blend K cold 160 13

arrange(): Descending Order

Default is ascending order!

cereal |> 
  arrange(calories)


Need to add desc() to get descending order!

cereal |> 
  arrange(desc(calories))

slice_max()

Selects the n rows with the maximum values of the specified variable.

cereal |> 
  slice_max(order_by = sugars, n = 3)
name manuf type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
Golden Crisp P cold 100 2 0 45 0 11 15 40 25 1 1.00 0.88 35.25244
Smacks K cold 110 2 1 70 1 9 15 40 25 2 1.00 0.75 31.23005
Apple Jacks K cold 110 2 0 125 1 11 14 30 25 2 1.00 1.00 33.17409
Post Nat. Raisin Bran P cold 120 3 1 200 6 11 14 260 25 3 1.33 0.67 37.84059
Total Raisin Bran G cold 140 3 1 190 4 15 14 230 100 3 1.50 1.00 28.59278
cereal |> 
  slice_max(order_by = sugars, n = 3, with_ties = FALSE)

How do we “arrange” in base R?

You can use the order() function!

cereal[order(cereal$calories),]
name manuf type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
4 All-Bran with Extra Fiber K cold 50 4 0 140 14.0 8.0 0 330 25 3 1.00 0.50 93.70491
55 Puffed Rice Q cold 50 1 0 0 0.0 13.0 0 15 0 3 0.50 1.00 60.75611
56 Puffed Wheat Q cold 50 2 0 0 1.0 10.0 0 50 0 3 0.50 1.00 63.00565
1 100% Bran N cold 70 4 1 130 10.0 5.0 6 280 25 3 1.00 0.33 68.40297
3 All-Bran K cold 70 4 1 260 9.0 7.0 5 320 25 3 1.00 0.33 59.42551
64 Shredded Wheat N cold 80 2 0 0 3.0 16.0 0 95 0 1 0.83 1.00 68.23588
9 Bran Chex R cold 90 2 1 200 4.0 15.0 6 125 25 1 1.00 0.67 49.12025
10 Bran Flakes P cold 90 3 0 210 5.0 13.0 5 190 25 3 1.00 0.67 53.31381
51 Nutri-grain Wheat K cold 90 3 0 170 3.0 18.0 2 90 25 3 1.00 1.00 59.64284
61 Raisin Squares K cold 90 2 0 0 2.0 15.0 6 110 25 3 1.00 0.50 55.33314
65 Shredded Wheat 'n'Bran N cold 90 3 0 0 4.0 19.0 0 140 0 1 1.00 0.67 74.47295
66 Shredded Wheat spoon size N cold 90 3 0 0 3.0 20.0 0 120 0 1 1.00 0.67 72.80179
69 Strawberry Fruit Wheats N cold 90 2 0 15 3.0 15.0 5 90 25 2 1.00 1.00 59.36399
17 Corn Flakes K cold 100 2 0 290 1.0 21.0 2 35 25 1 1.00 1.00 45.86332
21 Cream of Wheat (Quick) N hot 100 3 0 80 1.0 21.0 0 -1 0 2 1.00 1.00 64.53382
23 Crispy Wheat & Raisins G cold 100 2 1 140 2.0 11.0 10 120 25 3 1.00 0.75 36.17620
24 Double Chex R cold 100 2 0 190 1.0 18.0 5 80 25 3 1.00 0.75 44.33086
27 Frosted Mini-Wheats K cold 100 3 0 0 3.0 14.0 7 100 25 2 1.00 0.80 58.34514
31 Golden Crisp P cold 100 2 0 45 0.0 11.0 15 40 25 1 1.00 0.88 35.25244
33 Grape Nuts Flakes P cold 100 3 1 140 3.0 15.0 5 85 25 3 1.00 0.88 52.07690
42 Life Q cold 100 4 2 150 2.0 12.0 6 95 25 2 1.00 0.67 45.32807
44 Maypo A hot 100 4 1 0 0.0 16.0 3 95 25 2 1.00 1.00 54.85092
48 Multi-Grain Cheerios G cold 100 2 1 220 2.0 15.0 6 90 25 1 1.00 1.00 40.10596
54 Product 19 K cold 100 3 0 320 1.0 20.0 3 45 100 3 1.00 1.00 41.50354
57 Quaker Oat Squares Q cold 100 4 1 135 2.0 14.0 6 110 25 3 1.00 0.50 49.51187
58 Quaker Oatmeal Q hot 100 5 2 0 2.7 -1.0 -1 110 0 1 1.00 0.67 50.82839
60 Raisin Nut Bran G cold 100 3 2 140 2.5 10.5 8 140 25 3 1.00 0.50 39.70340
72 Total Whole Grain G cold 100 3 1 200 3.0 16.0 3 110 100 3 1.00 1.00 46.65884
75 Wheat Chex R cold 100 3 1 230 3.0 17.0 3 115 25 1 1.00 0.67 49.78744
76 Wheaties G cold 100 3 1 200 3.0 17.0 3 110 25 1 1.00 1.00 51.59219
5 Almond Delight R cold 110 2 2 200 1.0 14.0 8 -1 25 3 1.00 0.75 34.38484
6 Apple Cinnamon Cheerios G cold 110 2 2 180 1.5 10.5 10 70 25 1 1.00 0.75 29.50954
7 Apple Jacks K cold 110 2 0 125 1.0 11.0 14 30 25 2 1.00 1.00 33.17409
12 Cheerios G cold 110 6 2 290 2.0 17.0 1 105 25 1 1.00 1.25 50.76500
14 Clusters G cold 110 3 2 140 2.0 13.0 7 105 25 3 1.00 0.50 40.40021
15 Cocoa Puffs G cold 110 1 1 180 0.0 12.0 13 55 25 2 1.00 1.00 22.73645
16 Corn Chex R cold 110 2 0 280 0.0 22.0 3 25 25 1 1.00 1.00 41.44502
18 Corn Pops K cold 110 1 0 90 1.0 13.0 12 20 25 2 1.00 1.00 35.78279
19 Count Chocula G cold 110 1 1 180 0.0 12.0 13 65 25 2 1.00 1.00 22.39651
20 Cracklin' Oat Bran K cold 110 3 3 140 4.0 10.0 7 160 25 3 1.00 0.50 40.44877
22 Crispix K cold 110 2 0 220 1.0 21.0 3 30 25 3 1.00 1.00 46.89564
25 Froot Loops K cold 110 2 1 125 1.0 11.0 13 30 25 2 1.00 1.00 32.20758
26 Frosted Flakes K cold 110 1 0 200 1.0 14.0 11 25 25 1 1.00 0.75 31.43597
30 Fruity Pebbles P cold 110 1 1 135 0.0 13.0 12 25 25 2 1.00 0.75 28.02576
32 Golden Grahams G cold 110 1 1 280 0.0 15.0 9 45 25 2 1.00 0.75 23.80404
34 Grape-Nuts P cold 110 3 0 170 3.0 17.0 3 90 25 3 1.00 0.25 53.37101
37 Honey Nut Cheerios G cold 110 3 1 250 1.5 11.5 10 90 25 1 1.00 0.75 31.07222
38 Honey-comb P cold 110 1 0 180 0.0 14.0 11 35 25 1 1.00 1.33 28.74241
39 Just Right Crunchy Nuggets K cold 110 2 1 170 1.0 17.0 6 60 100 3 1.00 1.00 36.52368
41 Kix G cold 110 2 1 260 0.0 21.0 3 40 25 2 1.00 1.50 39.24111
43 Lucky Charms G cold 110 2 1 180 0.0 12.0 12 55 25 2 1.00 1.00 26.73451
62 Rice Chex R cold 110 1 0 240 0.0 23.0 2 30 25 1 1.00 1.13 41.99893
63 Rice Krispies K cold 110 2 0 290 0.0 22.0 3 35 25 1 1.00 1.00 40.56016
67 Smacks K cold 110 2 1 70 1.0 9.0 15 40 25 2 1.00 0.75 31.23005
68 Special K K cold 110 6 0 230 1.0 16.0 3 55 25 1 1.00 1.00 53.13132
70 Total Corn Flakes G cold 110 2 1 200 0.0 21.0 3 35 100 3 1.00 1.00 38.83975
73 Triples G cold 110 2 1 250 0.0 21.0 3 60 25 3 1.00 0.75 39.10617
74 Trix G cold 110 1 1 140 0.0 13.0 12 25 25 2 1.00 1.00 27.75330
77 Wheaties Honey Gold G cold 110 2 1 200 1.0 16.0 8 60 25 1 1.00 0.75 36.18756
2 100% Natural Bran Q cold 120 3 5 15 2.0 8.0 8 135 0 3 1.00 1.00 33.98368
11 Cap'n'Crunch Q cold 120 1 2 220 0.0 12.0 12 35 25 2 1.00 0.75 18.04285
13 Cinnamon Toast Crunch G cold 120 1 3 210 0.0 13.0 9 45 25 2 1.00 0.75 19.82357
28 Fruit & Fibre Dates; Walnuts; and Oats P cold 120 3 2 160 5.0 12.0 10 200 25 3 1.25 0.67 40.91705
29 Fruitful Bran K cold 120 3 0 240 5.0 14.0 12 190 25 3 1.33 0.67 41.01549
35 Great Grains Pecan P cold 120 3 3 75 3.0 13.0 4 100 25 3 1.00 0.33 45.81172
36 Honey Graham Ohs Q cold 120 1 2 220 1.0 12.0 11 45 25 2 1.00 1.00 21.87129
49 Nut&Honey Crunch K cold 120 2 1 190 0.0 15.0 9 40 25 2 1.00 0.67 29.92429
53 Post Nat. Raisin Bran P cold 120 3 1 200 6.0 11.0 14 260 25 3 1.33 0.67 37.84059
59 Raisin Bran K cold 120 3 1 210 5.0 14.0 12 240 25 2 1.33 0.75 39.25920
8 Basic 4 G cold 130 3 2 210 2.0 18.0 8 100 25 3 1.33 0.75 37.03856
52 Oatmeal Raisin Crisp G cold 130 3 2 170 1.5 13.5 10 120 25 3 1.25 0.50 30.45084
40 Just Right Fruit & Nut K cold 140 3 1 170 2.0 20.0 9 95 100 3 1.30 0.75 36.47151
50 Nutri-Grain Almond-Raisin K cold 140 3 2 220 3.0 21.0 7 130 25 3 1.33 0.67 40.69232
71 Total Raisin Bran G cold 140 3 1 190 4.0 15.0 14 230 100 3 1.50 1.00 28.59278
45 Muesli Raisins; Dates; & Almonds R cold 150 4 3 95 3.0 16.0 11 170 25 3 1.00 1.00 37.13686
46 Muesli Raisins; Peaches; & Pecans R cold 150 4 3 150 3.0 16.0 11 170 25 3 1.00 1.00 34.13976
47 Mueslix Crispy Blend K cold 160 3 2 150 3.0 17.0 13 160 25 3 1.50 0.67 30.31335
cereal[order(cereal$calories, cereal$sugars),]
name manuf type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
4 All-Bran with Extra Fiber K cold 50 4 0 140 14.0 8.0 0 330 25 3 1.00 0.50 93.70491
55 Puffed Rice Q cold 50 1 0 0 0.0 13.0 0 15 0 3 0.50 1.00 60.75611
56 Puffed Wheat Q cold 50 2 0 0 1.0 10.0 0 50 0 3 0.50 1.00 63.00565
3 All-Bran K cold 70 4 1 260 9.0 7.0 5 320 25 3 1.00 0.33 59.42551
1 100% Bran N cold 70 4 1 130 10.0 5.0 6 280 25 3 1.00 0.33 68.40297
64 Shredded Wheat N cold 80 2 0 0 3.0 16.0 0 95 0 1 0.83 1.00 68.23588
65 Shredded Wheat 'n'Bran N cold 90 3 0 0 4.0 19.0 0 140 0 1 1.00 0.67 74.47295
66 Shredded Wheat spoon size N cold 90 3 0 0 3.0 20.0 0 120 0 1 1.00 0.67 72.80179
51 Nutri-grain Wheat K cold 90 3 0 170 3.0 18.0 2 90 25 3 1.00 1.00 59.64284
10 Bran Flakes P cold 90 3 0 210 5.0 13.0 5 190 25 3 1.00 0.67 53.31381
69 Strawberry Fruit Wheats N cold 90 2 0 15 3.0 15.0 5 90 25 2 1.00 1.00 59.36399
9 Bran Chex R cold 90 2 1 200 4.0 15.0 6 125 25 1 1.00 0.67 49.12025
61 Raisin Squares K cold 90 2 0 0 2.0 15.0 6 110 25 3 1.00 0.50 55.33314
58 Quaker Oatmeal Q hot 100 5 2 0 2.7 -1.0 -1 110 0 1 1.00 0.67 50.82839
21 Cream of Wheat (Quick) N hot 100 3 0 80 1.0 21.0 0 -1 0 2 1.00 1.00 64.53382
17 Corn Flakes K cold 100 2 0 290 1.0 21.0 2 35 25 1 1.00 1.00 45.86332
44 Maypo A hot 100 4 1 0 0.0 16.0 3 95 25 2 1.00 1.00 54.85092
54 Product 19 K cold 100 3 0 320 1.0 20.0 3 45 100 3 1.00 1.00 41.50354
72 Total Whole Grain G cold 100 3 1 200 3.0 16.0 3 110 100 3 1.00 1.00 46.65884
75 Wheat Chex R cold 100 3 1 230 3.0 17.0 3 115 25 1 1.00 0.67 49.78744
76 Wheaties G cold 100 3 1 200 3.0 17.0 3 110 25 1 1.00 1.00 51.59219
24 Double Chex R cold 100 2 0 190 1.0 18.0 5 80 25 3 1.00 0.75 44.33086
33 Grape Nuts Flakes P cold 100 3 1 140 3.0 15.0 5 85 25 3 1.00 0.88 52.07690
42 Life Q cold 100 4 2 150 2.0 12.0 6 95 25 2 1.00 0.67 45.32807
48 Multi-Grain Cheerios G cold 100 2 1 220 2.0 15.0 6 90 25 1 1.00 1.00 40.10596
57 Quaker Oat Squares Q cold 100 4 1 135 2.0 14.0 6 110 25 3 1.00 0.50 49.51187
27 Frosted Mini-Wheats K cold 100 3 0 0 3.0 14.0 7 100 25 2 1.00 0.80 58.34514
60 Raisin Nut Bran G cold 100 3 2 140 2.5 10.5 8 140 25 3 1.00 0.50 39.70340
23 Crispy Wheat & Raisins G cold 100 2 1 140 2.0 11.0 10 120 25 3 1.00 0.75 36.17620
31 Golden Crisp P cold 100 2 0 45 0.0 11.0 15 40 25 1 1.00 0.88 35.25244
12 Cheerios G cold 110 6 2 290 2.0 17.0 1 105 25 1 1.00 1.25 50.76500
62 Rice Chex R cold 110 1 0 240 0.0 23.0 2 30 25 1 1.00 1.13 41.99893
16 Corn Chex R cold 110 2 0 280 0.0 22.0 3 25 25 1 1.00 1.00 41.44502
22 Crispix K cold 110 2 0 220 1.0 21.0 3 30 25 3 1.00 1.00 46.89564
34 Grape-Nuts P cold 110 3 0 170 3.0 17.0 3 90 25 3 1.00 0.25 53.37101
41 Kix G cold 110 2 1 260 0.0 21.0 3 40 25 2 1.00 1.50 39.24111
63 Rice Krispies K cold 110 2 0 290 0.0 22.0 3 35 25 1 1.00 1.00 40.56016
68 Special K K cold 110 6 0 230 1.0 16.0 3 55 25 1 1.00 1.00 53.13132
70 Total Corn Flakes G cold 110 2 1 200 0.0 21.0 3 35 100 3 1.00 1.00 38.83975
73 Triples G cold 110 2 1 250 0.0 21.0 3 60 25 3 1.00 0.75 39.10617
39 Just Right Crunchy Nuggets K cold 110 2 1 170 1.0 17.0 6 60 100 3 1.00 1.00 36.52368
14 Clusters G cold 110 3 2 140 2.0 13.0 7 105 25 3 1.00 0.50 40.40021
20 Cracklin' Oat Bran K cold 110 3 3 140 4.0 10.0 7 160 25 3 1.00 0.50 40.44877
5 Almond Delight R cold 110 2 2 200 1.0 14.0 8 -1 25 3 1.00 0.75 34.38484
77 Wheaties Honey Gold G cold 110 2 1 200 1.0 16.0 8 60 25 1 1.00 0.75 36.18756
32 Golden Grahams G cold 110 1 1 280 0.0 15.0 9 45 25 2 1.00 0.75 23.80404
6 Apple Cinnamon Cheerios G cold 110 2 2 180 1.5 10.5 10 70 25 1 1.00 0.75 29.50954
37 Honey Nut Cheerios G cold 110 3 1 250 1.5 11.5 10 90 25 1 1.00 0.75 31.07222
26 Frosted Flakes K cold 110 1 0 200 1.0 14.0 11 25 25 1 1.00 0.75 31.43597
38 Honey-comb P cold 110 1 0 180 0.0 14.0 11 35 25 1 1.00 1.33 28.74241
18 Corn Pops K cold 110 1 0 90 1.0 13.0 12 20 25 2 1.00 1.00 35.78279
30 Fruity Pebbles P cold 110 1 1 135 0.0 13.0 12 25 25 2 1.00 0.75 28.02576
43 Lucky Charms G cold 110 2 1 180 0.0 12.0 12 55 25 2 1.00 1.00 26.73451
74 Trix G cold 110 1 1 140 0.0 13.0 12 25 25 2 1.00 1.00 27.75330
15 Cocoa Puffs G cold 110 1 1 180 0.0 12.0 13 55 25 2 1.00 1.00 22.73645
19 Count Chocula G cold 110 1 1 180 0.0 12.0 13 65 25 2 1.00 1.00 22.39651
25 Froot Loops K cold 110 2 1 125 1.0 11.0 13 30 25 2 1.00 1.00 32.20758
7 Apple Jacks K cold 110 2 0 125 1.0 11.0 14 30 25 2 1.00 1.00 33.17409
67 Smacks K cold 110 2 1 70 1.0 9.0 15 40 25 2 1.00 0.75 31.23005
35 Great Grains Pecan P cold 120 3 3 75 3.0 13.0 4 100 25 3 1.00 0.33 45.81172
2 100% Natural Bran Q cold 120 3 5 15 2.0 8.0 8 135 0 3 1.00 1.00 33.98368
13 Cinnamon Toast Crunch G cold 120 1 3 210 0.0 13.0 9 45 25 2 1.00 0.75 19.82357
49 Nut&Honey Crunch K cold 120 2 1 190 0.0 15.0 9 40 25 2 1.00 0.67 29.92429
28 Fruit & Fibre Dates; Walnuts; and Oats P cold 120 3 2 160 5.0 12.0 10 200 25 3 1.25 0.67 40.91705
36 Honey Graham Ohs Q cold 120 1 2 220 1.0 12.0 11 45 25 2 1.00 1.00 21.87129
11 Cap'n'Crunch Q cold 120 1 2 220 0.0 12.0 12 35 25 2 1.00 0.75 18.04285
29 Fruitful Bran K cold 120 3 0 240 5.0 14.0 12 190 25 3 1.33 0.67 41.01549
59 Raisin Bran K cold 120 3 1 210 5.0 14.0 12 240 25 2 1.33 0.75 39.25920
53 Post Nat. Raisin Bran P cold 120 3 1 200 6.0 11.0 14 260 25 3 1.33 0.67 37.84059
8 Basic 4 G cold 130 3 2 210 2.0 18.0 8 100 25 3 1.33 0.75 37.03856
52 Oatmeal Raisin Crisp G cold 130 3 2 170 1.5 13.5 10 120 25 3 1.25 0.50 30.45084
50 Nutri-Grain Almond-Raisin K cold 140 3 2 220 3.0 21.0 7 130 25 3 1.33 0.67 40.69232
40 Just Right Fruit & Nut K cold 140 3 1 170 2.0 20.0 9 95 100 3 1.30 0.75 36.47151
71 Total Raisin Bran G cold 140 3 1 190 4.0 15.0 14 230 100 3 1.50 1.00 28.59278
45 Muesli Raisins; Dates; & Almonds R cold 150 4 3 95 3.0 16.0 11 170 25 3 1.00 1.00 37.13686
46 Muesli Raisins; Peaches; & Pecans R cold 150 4 3 150 3.0 16.0 11 170 25 3 1.00 1.00 34.13976
47 Mueslix Crispy Blend K cold 160 3 2 150 3.0 17.0 13 160 25 3 1.50 0.67 30.31335

select()

select()

We select which variables we would like to remain in the data.

cereal |> 
  select(name, manuf, calories, cups)
name manuf calories cups
100% Bran N 70 0.33
100% Natural Bran Q 120 1.00
All-Bran K 70 0.33
All-Bran with Extra Fiber K 50 0.50
Almond Delight R 110 0.75
Apple Cinnamon Cheerios G 110 0.75
Apple Jacks K 110 1.00
Basic 4 G 130 0.75
Bran Chex R 90 0.67
Bran Flakes P 90 0.67
Cap'n'Crunch Q 120 0.75
Cheerios G 110 1.25
Cinnamon Toast Crunch G 120 0.75
Clusters G 110 0.50
Cocoa Puffs G 110 1.00
Corn Chex R 110 1.00
Corn Flakes K 100 1.00
Corn Pops K 110 1.00
Count Chocula G 110 1.00
Cracklin' Oat Bran K 110 0.50
Cream of Wheat (Quick) N 100 1.00
Crispix K 110 1.00
Crispy Wheat & Raisins G 100 0.75
Double Chex R 100 0.75
Froot Loops K 110 1.00
Frosted Flakes K 110 0.75
Frosted Mini-Wheats K 100 0.80
Fruit & Fibre Dates; Walnuts; and Oats P 120 0.67
Fruitful Bran K 120 0.67
Fruity Pebbles P 110 0.75
Golden Crisp P 100 0.88
Golden Grahams G 110 0.75
Grape Nuts Flakes P 100 0.88
Grape-Nuts P 110 0.25
Great Grains Pecan P 120 0.33
Honey Graham Ohs Q 120 1.00
Honey Nut Cheerios G 110 0.75
Honey-comb P 110 1.33
Just Right Crunchy Nuggets K 110 1.00
Just Right Fruit & Nut K 140 0.75
Kix G 110 1.50
Life Q 100 0.67
Lucky Charms G 110 1.00
Maypo A 100 1.00
Muesli Raisins; Dates; & Almonds R 150 1.00
Muesli Raisins; Peaches; & Pecans R 150 1.00
Mueslix Crispy Blend K 160 0.67
Multi-Grain Cheerios G 100 1.00
Nut&Honey Crunch K 120 0.67
Nutri-Grain Almond-Raisin K 140 0.67
Nutri-grain Wheat K 90 1.00
Oatmeal Raisin Crisp G 130 0.50
Post Nat. Raisin Bran P 120 0.67
Product 19 K 100 1.00
Puffed Rice Q 50 1.00
Puffed Wheat Q 50 1.00
Quaker Oat Squares Q 100 0.50
Quaker Oatmeal Q 100 0.67
Raisin Bran K 120 0.75
Raisin Nut Bran G 100 0.50
Raisin Squares K 90 0.50
Rice Chex R 110 1.13
Rice Krispies K 110 1.00
Shredded Wheat N 80 1.00
Shredded Wheat 'n'Bran N 90 0.67
Shredded Wheat spoon size N 90 0.67
Smacks K 110 0.75
Special K K 110 1.00
Strawberry Fruit Wheats N 90 1.00
Total Corn Flakes G 110 1.00
Total Raisin Bran G 140 1.00
Total Whole Grain G 100 1.00
Triples G 110 0.75
Trix G 110 1.00
Wheat Chex R 100 0.67
Wheaties G 100 1.00
Wheaties Honey Gold G 110 0.75

select()

You can use : to select a sequence of columns!

cereal |> 
  select(name:calories)
name manuf type calories
100% Bran N cold 70
100% Natural Bran Q cold 120
All-Bran K cold 70
All-Bran with Extra Fiber K cold 50
Almond Delight R cold 110
Apple Cinnamon Cheerios G cold 110
Apple Jacks K cold 110
Basic 4 G cold 130
Bran Chex R cold 90
Bran Flakes P cold 90
Cap'n'Crunch Q cold 120
Cheerios G cold 110
Cinnamon Toast Crunch G cold 120
Clusters G cold 110
Cocoa Puffs G cold 110
Corn Chex R cold 110
Corn Flakes K cold 100
Corn Pops K cold 110
Count Chocula G cold 110
Cracklin' Oat Bran K cold 110
Cream of Wheat (Quick) N hot 100
Crispix K cold 110
Crispy Wheat & Raisins G cold 100
Double Chex R cold 100
Froot Loops K cold 110
Frosted Flakes K cold 110
Frosted Mini-Wheats K cold 100
Fruit & Fibre Dates; Walnuts; and Oats P cold 120
Fruitful Bran K cold 120
Fruity Pebbles P cold 110
Golden Crisp P cold 100
Golden Grahams G cold 110
Grape Nuts Flakes P cold 100
Grape-Nuts P cold 110
Great Grains Pecan P cold 120
Honey Graham Ohs Q cold 120
Honey Nut Cheerios G cold 110
Honey-comb P cold 110
Just Right Crunchy Nuggets K cold 110
Just Right Fruit & Nut K cold 140
Kix G cold 110
Life Q cold 100
Lucky Charms G cold 110
Maypo A hot 100
Muesli Raisins; Dates; & Almonds R cold 150
Muesli Raisins; Peaches; & Pecans R cold 150
Mueslix Crispy Blend K cold 160
Multi-Grain Cheerios G cold 100
Nut&Honey Crunch K cold 120
Nutri-Grain Almond-Raisin K cold 140
Nutri-grain Wheat K cold 90
Oatmeal Raisin Crisp G cold 130
Post Nat. Raisin Bran P cold 120
Product 19 K cold 100
Puffed Rice Q cold 50
Puffed Wheat Q cold 50
Quaker Oat Squares Q cold 100
Quaker Oatmeal Q hot 100
Raisin Bran K cold 120
Raisin Nut Bran G cold 100
Raisin Squares K cold 90
Rice Chex R cold 110
Rice Krispies K cold 110
Shredded Wheat N cold 80
Shredded Wheat 'n'Bran N cold 90
Shredded Wheat spoon size N cold 90
Smacks K cold 110
Special K K cold 110
Strawberry Fruit Wheats N cold 90
Total Corn Flakes G cold 110
Total Raisin Bran G cold 140
Total Whole Grain G cold 100
Triples G cold 110
Trix G cold 110
Wheat Chex R cold 100
Wheaties G cold 100
Wheaties Honey Gold G cold 110

You can remove columns from the dataset using a -.

cereal |> 
  select(-rating)

select(): Reordering

You can reorder columns inside of select()!

cereal |> 
  select(name, rating, manuf, type, calories, cups, weight,
         everything())
name rating manuf type calories cups weight protein fat sodium fiber carbo sugars potass vitamins shelf
100% Bran 68.40297 N cold 70 0.33 1.00 4 1 130 10.0 5.0 6 280 25 3
100% Natural Bran 33.98368 Q cold 120 1.00 1.00 3 5 15 2.0 8.0 8 135 0 3
All-Bran 59.42551 K cold 70 0.33 1.00 4 1 260 9.0 7.0 5 320 25 3
All-Bran with Extra Fiber 93.70491 K cold 50 0.50 1.00 4 0 140 14.0 8.0 0 330 25 3
Almond Delight 34.38484 R cold 110 0.75 1.00 2 2 200 1.0 14.0 8 -1 25 3
Apple Cinnamon Cheerios 29.50954 G cold 110 0.75 1.00 2 2 180 1.5 10.5 10 70 25 1
Apple Jacks 33.17409 K cold 110 1.00 1.00 2 0 125 1.0 11.0 14 30 25 2
Basic 4 37.03856 G cold 130 0.75 1.33 3 2 210 2.0 18.0 8 100 25 3
Bran Chex 49.12025 R cold 90 0.67 1.00 2 1 200 4.0 15.0 6 125 25 1
Bran Flakes 53.31381 P cold 90 0.67 1.00 3 0 210 5.0 13.0 5 190 25 3
Cap'n'Crunch 18.04285 Q cold 120 0.75 1.00 1 2 220 0.0 12.0 12 35 25 2
Cheerios 50.76500 G cold 110 1.25 1.00 6 2 290 2.0 17.0 1 105 25 1
Cinnamon Toast Crunch 19.82357 G cold 120 0.75 1.00 1 3 210 0.0 13.0 9 45 25 2
Clusters 40.40021 G cold 110 0.50 1.00 3 2 140 2.0 13.0 7 105 25 3
Cocoa Puffs 22.73645 G cold 110 1.00 1.00 1 1 180 0.0 12.0 13 55 25 2
Corn Chex 41.44502 R cold 110 1.00 1.00 2 0 280 0.0 22.0 3 25 25 1
Corn Flakes 45.86332 K cold 100 1.00 1.00 2 0 290 1.0 21.0 2 35 25 1
Corn Pops 35.78279 K cold 110 1.00 1.00 1 0 90 1.0 13.0 12 20 25 2
Count Chocula 22.39651 G cold 110 1.00 1.00 1 1 180 0.0 12.0 13 65 25 2
Cracklin' Oat Bran 40.44877 K cold 110 0.50 1.00 3 3 140 4.0 10.0 7 160 25 3
Cream of Wheat (Quick) 64.53382 N hot 100 1.00 1.00 3 0 80 1.0 21.0 0 -1 0 2
Crispix 46.89564 K cold 110 1.00 1.00 2 0 220 1.0 21.0 3 30 25 3
Crispy Wheat & Raisins 36.17620 G cold 100 0.75 1.00 2 1 140 2.0 11.0 10 120 25 3
Double Chex 44.33086 R cold 100 0.75 1.00 2 0 190 1.0 18.0 5 80 25 3
Froot Loops 32.20758 K cold 110 1.00 1.00 2 1 125 1.0 11.0 13 30 25 2
Frosted Flakes 31.43597 K cold 110 0.75 1.00 1 0 200 1.0 14.0 11 25 25 1
Frosted Mini-Wheats 58.34514 K cold 100 0.80 1.00 3 0 0 3.0 14.0 7 100 25 2
Fruit & Fibre Dates; Walnuts; and Oats 40.91705 P cold 120 0.67 1.25 3 2 160 5.0 12.0 10 200 25 3
Fruitful Bran 41.01549 K cold 120 0.67 1.33 3 0 240 5.0 14.0 12 190 25 3
Fruity Pebbles 28.02576 P cold 110 0.75 1.00 1 1 135 0.0 13.0 12 25 25 2
Golden Crisp 35.25244 P cold 100 0.88 1.00 2 0 45 0.0 11.0 15 40 25 1
Golden Grahams 23.80404 G cold 110 0.75 1.00 1 1 280 0.0 15.0 9 45 25 2
Grape Nuts Flakes 52.07690 P cold 100 0.88 1.00 3 1 140 3.0 15.0 5 85 25 3
Grape-Nuts 53.37101 P cold 110 0.25 1.00 3 0 170 3.0 17.0 3 90 25 3
Great Grains Pecan 45.81172 P cold 120 0.33 1.00 3 3 75 3.0 13.0 4 100 25 3
Honey Graham Ohs 21.87129 Q cold 120 1.00 1.00 1 2 220 1.0 12.0 11 45 25 2
Honey Nut Cheerios 31.07222 G cold 110 0.75 1.00 3 1 250 1.5 11.5 10 90 25 1
Honey-comb 28.74241 P cold 110 1.33 1.00 1 0 180 0.0 14.0 11 35 25 1
Just Right Crunchy Nuggets 36.52368 K cold 110 1.00 1.00 2 1 170 1.0 17.0 6 60 100 3
Just Right Fruit & Nut 36.47151 K cold 140 0.75 1.30 3 1 170 2.0 20.0 9 95 100 3
Kix 39.24111 G cold 110 1.50 1.00 2 1 260 0.0 21.0 3 40 25 2
Life 45.32807 Q cold 100 0.67 1.00 4 2 150 2.0 12.0 6 95 25 2
Lucky Charms 26.73451 G cold 110 1.00 1.00 2 1 180 0.0 12.0 12 55 25 2
Maypo 54.85092 A hot 100 1.00 1.00 4 1 0 0.0 16.0 3 95 25 2
Muesli Raisins; Dates; & Almonds 37.13686 R cold 150 1.00 1.00 4 3 95 3.0 16.0 11 170 25 3
Muesli Raisins; Peaches; & Pecans 34.13976 R cold 150 1.00 1.00 4 3 150 3.0 16.0 11 170 25 3
Mueslix Crispy Blend 30.31335 K cold 160 0.67 1.50 3 2 150 3.0 17.0 13 160 25 3
Multi-Grain Cheerios 40.10596 G cold 100 1.00 1.00 2 1 220 2.0 15.0 6 90 25 1
Nut&Honey Crunch 29.92429 K cold 120 0.67 1.00 2 1 190 0.0 15.0 9 40 25 2
Nutri-Grain Almond-Raisin 40.69232 K cold 140 0.67 1.33 3 2 220 3.0 21.0 7 130 25 3
Nutri-grain Wheat 59.64284 K cold 90 1.00 1.00 3 0 170 3.0 18.0 2 90 25 3
Oatmeal Raisin Crisp 30.45084 G cold 130 0.50 1.25 3 2 170 1.5 13.5 10 120 25 3
Post Nat. Raisin Bran 37.84059 P cold 120 0.67 1.33 3 1 200 6.0 11.0 14 260 25 3
Product 19 41.50354 K cold 100 1.00 1.00 3 0 320 1.0 20.0 3 45 100 3
Puffed Rice 60.75611 Q cold 50 1.00 0.50 1 0 0 0.0 13.0 0 15 0 3
Puffed Wheat 63.00565 Q cold 50 1.00 0.50 2 0 0 1.0 10.0 0 50 0 3
Quaker Oat Squares 49.51187 Q cold 100 0.50 1.00 4 1 135 2.0 14.0 6 110 25 3
Quaker Oatmeal 50.82839 Q hot 100 0.67 1.00 5 2 0 2.7 -1.0 -1 110 0 1
Raisin Bran 39.25920 K cold 120 0.75 1.33 3 1 210 5.0 14.0 12 240 25 2
Raisin Nut Bran 39.70340 G cold 100 0.50 1.00 3 2 140 2.5 10.5 8 140 25 3
Raisin Squares 55.33314 K cold 90 0.50 1.00 2 0 0 2.0 15.0 6 110 25 3
Rice Chex 41.99893 R cold 110 1.13 1.00 1 0 240 0.0 23.0 2 30 25 1
Rice Krispies 40.56016 K cold 110 1.00 1.00 2 0 290 0.0 22.0 3 35 25 1
Shredded Wheat 68.23588 N cold 80 1.00 0.83 2 0 0 3.0 16.0 0 95 0 1
Shredded Wheat 'n'Bran 74.47295 N cold 90 0.67 1.00 3 0 0 4.0 19.0 0 140 0 1
Shredded Wheat spoon size 72.80179 N cold 90 0.67 1.00 3 0 0 3.0 20.0 0 120 0 1
Smacks 31.23005 K cold 110 0.75 1.00 2 1 70 1.0 9.0 15 40 25 2
Special K 53.13132 K cold 110 1.00 1.00 6 0 230 1.0 16.0 3 55 25 1
Strawberry Fruit Wheats 59.36399 N cold 90 1.00 1.00 2 0 15 3.0 15.0 5 90 25 2
Total Corn Flakes 38.83975 G cold 110 1.00 1.00 2 1 200 0.0 21.0 3 35 100 3
Total Raisin Bran 28.59278 G cold 140 1.00 1.50 3 1 190 4.0 15.0 14 230 100 3
Total Whole Grain 46.65884 G cold 100 1.00 1.00 3 1 200 3.0 16.0 3 110 100 3
Triples 39.10617 G cold 110 0.75 1.00 2 1 250 0.0 21.0 3 60 25 3
Trix 27.75330 G cold 110 1.00 1.00 1 1 140 0.0 13.0 12 25 25 2
Wheat Chex 49.78744 R cold 100 0.67 1.00 3 1 230 3.0 17.0 3 115 25 1
Wheaties 51.59219 G cold 100 1.00 1.00 3 1 200 3.0 17.0 3 110 25 1
Wheaties Honey Gold 36.18756 G cold 110 0.75 1.00 2 1 200 1.0 16.0 8 60 25 1

select(): Handy Helpers!

  • everything() – selects all columns that you have not already specified
  • starts_with() – selects columns with names that start with the specified string
  • ends_with() – selects columns with names that end with the specified string
  • contains() – selects columns with names that contain the specified string

rename()

  • You can rename columns with select(), but all columns not specified will be dropped.
    • Using the rename() function is easier!
cereal |> 
  rename(temp = type)
name manuf temp calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
100% Bran N cold 70 4 1 130 10.0 5.0 6 280 25 3 1.00 0.33 68.40297
100% Natural Bran Q cold 120 3 5 15 2.0 8.0 8 135 0 3 1.00 1.00 33.98368
All-Bran K cold 70 4 1 260 9.0 7.0 5 320 25 3 1.00 0.33 59.42551
All-Bran with Extra Fiber K cold 50 4 0 140 14.0 8.0 0 330 25 3 1.00 0.50 93.70491
Almond Delight R cold 110 2 2 200 1.0 14.0 8 -1 25 3 1.00 0.75 34.38484
Apple Cinnamon Cheerios G cold 110 2 2 180 1.5 10.5 10 70 25 1 1.00 0.75 29.50954
Apple Jacks K cold 110 2 0 125 1.0 11.0 14 30 25 2 1.00 1.00 33.17409
Basic 4 G cold 130 3 2 210 2.0 18.0 8 100 25 3 1.33 0.75 37.03856
Bran Chex R cold 90 2 1 200 4.0 15.0 6 125 25 1 1.00 0.67 49.12025
Bran Flakes P cold 90 3 0 210 5.0 13.0 5 190 25 3 1.00 0.67 53.31381
Cap'n'Crunch Q cold 120 1 2 220 0.0 12.0 12 35 25 2 1.00 0.75 18.04285
Cheerios G cold 110 6 2 290 2.0 17.0 1 105 25 1 1.00 1.25 50.76500
Cinnamon Toast Crunch G cold 120 1 3 210 0.0 13.0 9 45 25 2 1.00 0.75 19.82357
Clusters G cold 110 3 2 140 2.0 13.0 7 105 25 3 1.00 0.50 40.40021
Cocoa Puffs G cold 110 1 1 180 0.0 12.0 13 55 25 2 1.00 1.00 22.73645
Corn Chex R cold 110 2 0 280 0.0 22.0 3 25 25 1 1.00 1.00 41.44502
Corn Flakes K cold 100 2 0 290 1.0 21.0 2 35 25 1 1.00 1.00 45.86332
Corn Pops K cold 110 1 0 90 1.0 13.0 12 20 25 2 1.00 1.00 35.78279
Count Chocula G cold 110 1 1 180 0.0 12.0 13 65 25 2 1.00 1.00 22.39651
Cracklin' Oat Bran K cold 110 3 3 140 4.0 10.0 7 160 25 3 1.00 0.50 40.44877
Cream of Wheat (Quick) N hot 100 3 0 80 1.0 21.0 0 -1 0 2 1.00 1.00 64.53382
Crispix K cold 110 2 0 220 1.0 21.0 3 30 25 3 1.00 1.00 46.89564
Crispy Wheat & Raisins G cold 100 2 1 140 2.0 11.0 10 120 25 3 1.00 0.75 36.17620
Double Chex R cold 100 2 0 190 1.0 18.0 5 80 25 3 1.00 0.75 44.33086
Froot Loops K cold 110 2 1 125 1.0 11.0 13 30 25 2 1.00 1.00 32.20758
Frosted Flakes K cold 110 1 0 200 1.0 14.0 11 25 25 1 1.00 0.75 31.43597
Frosted Mini-Wheats K cold 100 3 0 0 3.0 14.0 7 100 25 2 1.00 0.80 58.34514
Fruit & Fibre Dates; Walnuts; and Oats P cold 120 3 2 160 5.0 12.0 10 200 25 3 1.25 0.67 40.91705
Fruitful Bran K cold 120 3 0 240 5.0 14.0 12 190 25 3 1.33 0.67 41.01549
Fruity Pebbles P cold 110 1 1 135 0.0 13.0 12 25 25 2 1.00 0.75 28.02576
Golden Crisp P cold 100 2 0 45 0.0 11.0 15 40 25 1 1.00 0.88 35.25244
Golden Grahams G cold 110 1 1 280 0.0 15.0 9 45 25 2 1.00 0.75 23.80404
Grape Nuts Flakes P cold 100 3 1 140 3.0 15.0 5 85 25 3 1.00 0.88 52.07690
Grape-Nuts P cold 110 3 0 170 3.0 17.0 3 90 25 3 1.00 0.25 53.37101
Great Grains Pecan P cold 120 3 3 75 3.0 13.0 4 100 25 3 1.00 0.33 45.81172
Honey Graham Ohs Q cold 120 1 2 220 1.0 12.0 11 45 25 2 1.00 1.00 21.87129
Honey Nut Cheerios G cold 110 3 1 250 1.5 11.5 10 90 25 1 1.00 0.75 31.07222
Honey-comb P cold 110 1 0 180 0.0 14.0 11 35 25 1 1.00 1.33 28.74241
Just Right Crunchy Nuggets K cold 110 2 1 170 1.0 17.0 6 60 100 3 1.00 1.00 36.52368
Just Right Fruit & Nut K cold 140 3 1 170 2.0 20.0 9 95 100 3 1.30 0.75 36.47151
Kix G cold 110 2 1 260 0.0 21.0 3 40 25 2 1.00 1.50 39.24111
Life Q cold 100 4 2 150 2.0 12.0 6 95 25 2 1.00 0.67 45.32807
Lucky Charms G cold 110 2 1 180 0.0 12.0 12 55 25 2 1.00 1.00 26.73451
Maypo A hot 100 4 1 0 0.0 16.0 3 95 25 2 1.00 1.00 54.85092
Muesli Raisins; Dates; & Almonds R cold 150 4 3 95 3.0 16.0 11 170 25 3 1.00 1.00 37.13686
Muesli Raisins; Peaches; & Pecans R cold 150 4 3 150 3.0 16.0 11 170 25 3 1.00 1.00 34.13976
Mueslix Crispy Blend K cold 160 3 2 150 3.0 17.0 13 160 25 3 1.50 0.67 30.31335
Multi-Grain Cheerios G cold 100 2 1 220 2.0 15.0 6 90 25 1 1.00 1.00 40.10596
Nut&Honey Crunch K cold 120 2 1 190 0.0 15.0 9 40 25 2 1.00 0.67 29.92429
Nutri-Grain Almond-Raisin K cold 140 3 2 220 3.0 21.0 7 130 25 3 1.33 0.67 40.69232
Nutri-grain Wheat K cold 90 3 0 170 3.0 18.0 2 90 25 3 1.00 1.00 59.64284
Oatmeal Raisin Crisp G cold 130 3 2 170 1.5 13.5 10 120 25 3 1.25 0.50 30.45084
Post Nat. Raisin Bran P cold 120 3 1 200 6.0 11.0 14 260 25 3 1.33 0.67 37.84059
Product 19 K cold 100 3 0 320 1.0 20.0 3 45 100 3 1.00 1.00 41.50354
Puffed Rice Q cold 50 1 0 0 0.0 13.0 0 15 0 3 0.50 1.00 60.75611
Puffed Wheat Q cold 50 2 0 0 1.0 10.0 0 50 0 3 0.50 1.00 63.00565
Quaker Oat Squares Q cold 100 4 1 135 2.0 14.0 6 110 25 3 1.00 0.50 49.51187
Quaker Oatmeal Q hot 100 5 2 0 2.7 -1.0 -1 110 0 1 1.00 0.67 50.82839
Raisin Bran K cold 120 3 1 210 5.0 14.0 12 240 25 2 1.33 0.75 39.25920
Raisin Nut Bran G cold 100 3 2 140 2.5 10.5 8 140 25 3 1.00 0.50 39.70340
Raisin Squares K cold 90 2 0 0 2.0 15.0 6 110 25 3 1.00 0.50 55.33314
Rice Chex R cold 110 1 0 240 0.0 23.0 2 30 25 1 1.00 1.13 41.99893
Rice Krispies K cold 110 2 0 290 0.0 22.0 3 35 25 1 1.00 1.00 40.56016
Shredded Wheat N cold 80 2 0 0 3.0 16.0 0 95 0 1 0.83 1.00 68.23588
Shredded Wheat 'n'Bran N cold 90 3 0 0 4.0 19.0 0 140 0 1 1.00 0.67 74.47295
Shredded Wheat spoon size N cold 90 3 0 0 3.0 20.0 0 120 0 1 1.00 0.67 72.80179
Smacks K cold 110 2 1 70 1.0 9.0 15 40 25 2 1.00 0.75 31.23005
Special K K cold 110 6 0 230 1.0 16.0 3 55 25 1 1.00 1.00 53.13132
Strawberry Fruit Wheats N cold 90 2 0 15 3.0 15.0 5 90 25 2 1.00 1.00 59.36399
Total Corn Flakes G cold 110 2 1 200 0.0 21.0 3 35 100 3 1.00 1.00 38.83975
Total Raisin Bran G cold 140 3 1 190 4.0 15.0 14 230 100 3 1.50 1.00 28.59278
Total Whole Grain G cold 100 3 1 200 3.0 16.0 3 110 100 3 1.00 1.00 46.65884
Triples G cold 110 2 1 250 0.0 21.0 3 60 25 3 1.00 0.75 39.10617
Trix G cold 110 1 1 140 0.0 13.0 12 25 25 2 1.00 1.00 27.75330
Wheat Chex R cold 100 3 1 230 3.0 17.0 3 115 25 1 1.00 0.67 49.78744
Wheaties G cold 100 3 1 200 3.0 17.0 3 110 25 1 1.00 1.00 51.59219
Wheaties Honey Gold G cold 110 2 1 200 1.0 16.0 8 60 25 1 1.00 0.75 36.18756

How do we “select” in base R?

You don’t really use a specific function!

cereal[,c("name", "manuf", "calories", "cups")]
name manuf calories cups
100% Bran N 70 0.33
100% Natural Bran Q 120 1.00
All-Bran K 70 0.33
All-Bran with Extra Fiber K 50 0.50
Almond Delight R 110 0.75
Apple Cinnamon Cheerios G 110 0.75
Apple Jacks K 110 1.00
Basic 4 G 130 0.75
Bran Chex R 90 0.67
Bran Flakes P 90 0.67
Cap'n'Crunch Q 120 0.75
Cheerios G 110 1.25
Cinnamon Toast Crunch G 120 0.75
Clusters G 110 0.50
Cocoa Puffs G 110 1.00
Corn Chex R 110 1.00
Corn Flakes K 100 1.00
Corn Pops K 110 1.00
Count Chocula G 110 1.00
Cracklin' Oat Bran K 110 0.50
Cream of Wheat (Quick) N 100 1.00
Crispix K 110 1.00
Crispy Wheat & Raisins G 100 0.75
Double Chex R 100 0.75
Froot Loops K 110 1.00
Frosted Flakes K 110 0.75
Frosted Mini-Wheats K 100 0.80
Fruit & Fibre Dates; Walnuts; and Oats P 120 0.67
Fruitful Bran K 120 0.67
Fruity Pebbles P 110 0.75
Golden Crisp P 100 0.88
Golden Grahams G 110 0.75
Grape Nuts Flakes P 100 0.88
Grape-Nuts P 110 0.25
Great Grains Pecan P 120 0.33
Honey Graham Ohs Q 120 1.00
Honey Nut Cheerios G 110 0.75
Honey-comb P 110 1.33
Just Right Crunchy Nuggets K 110 1.00
Just Right Fruit & Nut K 140 0.75
Kix G 110 1.50
Life Q 100 0.67
Lucky Charms G 110 1.00
Maypo A 100 1.00
Muesli Raisins; Dates; & Almonds R 150 1.00
Muesli Raisins; Peaches; & Pecans R 150 1.00
Mueslix Crispy Blend K 160 0.67
Multi-Grain Cheerios G 100 1.00
Nut&Honey Crunch K 120 0.67
Nutri-Grain Almond-Raisin K 140 0.67
Nutri-grain Wheat K 90 1.00
Oatmeal Raisin Crisp G 130 0.50
Post Nat. Raisin Bran P 120 0.67
Product 19 K 100 1.00
Puffed Rice Q 50 1.00
Puffed Wheat Q 50 1.00
Quaker Oat Squares Q 100 0.50
Quaker Oatmeal Q 100 0.67
Raisin Bran K 120 0.75
Raisin Nut Bran G 100 0.50
Raisin Squares K 90 0.50
Rice Chex R 110 1.13
Rice Krispies K 110 1.00
Shredded Wheat N 80 1.00
Shredded Wheat 'n'Bran N 90 0.67
Shredded Wheat spoon size N 90 0.67
Smacks K 110 0.75
Special K K 110 1.00
Strawberry Fruit Wheats N 90 1.00
Total Corn Flakes G 110 1.00
Total Raisin Bran G 140 1.00
Total Whole Grain G 100 1.00
Triples G 110 0.75
Trix G 110 1.00
Wheat Chex R 100 0.67
Wheaties G 100 1.00
Wheaties Honey Gold G 110 0.75
subset(cereal, select = -c(rating))


colnames(cereal)[2:4] <- c("maker","temp","cals")

mutate()

Mutate (by Allison Horst)

mutate()

The data set gets mutated to either include a new variable

cereal |> 
  mutate(cal_per_cup = calories / cups)
name manuf type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating cal_per_cup
100% Bran N cold 70 4 1 130 10.0 5.0 6 280 25 3 1.00 0.33 68.40297 212.12121
100% Natural Bran Q cold 120 3 5 15 2.0 8.0 8 135 0 3 1.00 1.00 33.98368 120.00000
All-Bran K cold 70 4 1 260 9.0 7.0 5 320 25 3 1.00 0.33 59.42551 212.12121
All-Bran with Extra Fiber K cold 50 4 0 140 14.0 8.0 0 330 25 3 1.00 0.50 93.70491 100.00000
Almond Delight R cold 110 2 2 200 1.0 14.0 8 -1 25 3 1.00 0.75 34.38484 146.66667
Apple Cinnamon Cheerios G cold 110 2 2 180 1.5 10.5 10 70 25 1 1.00 0.75 29.50954 146.66667
Apple Jacks K cold 110 2 0 125 1.0 11.0 14 30 25 2 1.00 1.00 33.17409 110.00000
Basic 4 G cold 130 3 2 210 2.0 18.0 8 100 25 3 1.33 0.75 37.03856 173.33333
Bran Chex R cold 90 2 1 200 4.0 15.0 6 125 25 1 1.00 0.67 49.12025 134.32836
Bran Flakes P cold 90 3 0 210 5.0 13.0 5 190 25 3 1.00 0.67 53.31381 134.32836
Cap'n'Crunch Q cold 120 1 2 220 0.0 12.0 12 35 25 2 1.00 0.75 18.04285 160.00000
Cheerios G cold 110 6 2 290 2.0 17.0 1 105 25 1 1.00 1.25 50.76500 88.00000
Cinnamon Toast Crunch G cold 120 1 3 210 0.0 13.0 9 45 25 2 1.00 0.75 19.82357 160.00000
Clusters G cold 110 3 2 140 2.0 13.0 7 105 25 3 1.00 0.50 40.40021 220.00000
Cocoa Puffs G cold 110 1 1 180 0.0 12.0 13 55 25 2 1.00 1.00 22.73645 110.00000
Corn Chex R cold 110 2 0 280 0.0 22.0 3 25 25 1 1.00 1.00 41.44502 110.00000
Corn Flakes K cold 100 2 0 290 1.0 21.0 2 35 25 1 1.00 1.00 45.86332 100.00000
Corn Pops K cold 110 1 0 90 1.0 13.0 12 20 25 2 1.00 1.00 35.78279 110.00000
Count Chocula G cold 110 1 1 180 0.0 12.0 13 65 25 2 1.00 1.00 22.39651 110.00000
Cracklin' Oat Bran K cold 110 3 3 140 4.0 10.0 7 160 25 3 1.00 0.50 40.44877 220.00000
Cream of Wheat (Quick) N hot 100 3 0 80 1.0 21.0 0 -1 0 2 1.00 1.00 64.53382 100.00000
Crispix K cold 110 2 0 220 1.0 21.0 3 30 25 3 1.00 1.00 46.89564 110.00000
Crispy Wheat & Raisins G cold 100 2 1 140 2.0 11.0 10 120 25 3 1.00 0.75 36.17620 133.33333
Double Chex R cold 100 2 0 190 1.0 18.0 5 80 25 3 1.00 0.75 44.33086 133.33333
Froot Loops K cold 110 2 1 125 1.0 11.0 13 30 25 2 1.00 1.00 32.20758 110.00000
Frosted Flakes K cold 110 1 0 200 1.0 14.0 11 25 25 1 1.00 0.75 31.43597 146.66667
Frosted Mini-Wheats K cold 100 3 0 0 3.0 14.0 7 100 25 2 1.00 0.80 58.34514 125.00000
Fruit & Fibre Dates; Walnuts; and Oats P cold 120 3 2 160 5.0 12.0 10 200 25 3 1.25 0.67 40.91705 179.10448
Fruitful Bran K cold 120 3 0 240 5.0 14.0 12 190 25 3 1.33 0.67 41.01549 179.10448
Fruity Pebbles P cold 110 1 1 135 0.0 13.0 12 25 25 2 1.00 0.75 28.02576 146.66667
Golden Crisp P cold 100 2 0 45 0.0 11.0 15 40 25 1 1.00 0.88 35.25244 113.63636
Golden Grahams G cold 110 1 1 280 0.0 15.0 9 45 25 2 1.00 0.75 23.80404 146.66667
Grape Nuts Flakes P cold 100 3 1 140 3.0 15.0 5 85 25 3 1.00 0.88 52.07690 113.63636
Grape-Nuts P cold 110 3 0 170 3.0 17.0 3 90 25 3 1.00 0.25 53.37101 440.00000
Great Grains Pecan P cold 120 3 3 75 3.0 13.0 4 100 25 3 1.00 0.33 45.81172 363.63636
Honey Graham Ohs Q cold 120 1 2 220 1.0 12.0 11 45 25 2 1.00 1.00 21.87129 120.00000
Honey Nut Cheerios G cold 110 3 1 250 1.5 11.5 10 90 25 1 1.00 0.75 31.07222 146.66667
Honey-comb P cold 110 1 0 180 0.0 14.0 11 35 25 1 1.00 1.33 28.74241 82.70677
Just Right Crunchy Nuggets K cold 110 2 1 170 1.0 17.0 6 60 100 3 1.00 1.00 36.52368 110.00000
Just Right Fruit & Nut K cold 140 3 1 170 2.0 20.0 9 95 100 3 1.30 0.75 36.47151 186.66667
Kix G cold 110 2 1 260 0.0 21.0 3 40 25 2 1.00 1.50 39.24111 73.33333
Life Q cold 100 4 2 150 2.0 12.0 6 95 25 2 1.00 0.67 45.32807 149.25373
Lucky Charms G cold 110 2 1 180 0.0 12.0 12 55 25 2 1.00 1.00 26.73451 110.00000
Maypo A hot 100 4 1 0 0.0 16.0 3 95 25 2 1.00 1.00 54.85092 100.00000
Muesli Raisins; Dates; & Almonds R cold 150 4 3 95 3.0 16.0 11 170 25 3 1.00 1.00 37.13686 150.00000
Muesli Raisins; Peaches; & Pecans R cold 150 4 3 150 3.0 16.0 11 170 25 3 1.00 1.00 34.13976 150.00000
Mueslix Crispy Blend K cold 160 3 2 150 3.0 17.0 13 160 25 3 1.50 0.67 30.31335 238.80597
Multi-Grain Cheerios G cold 100 2 1 220 2.0 15.0 6 90 25 1 1.00 1.00 40.10596 100.00000
Nut&Honey Crunch K cold 120 2 1 190 0.0 15.0 9 40 25 2 1.00 0.67 29.92429 179.10448
Nutri-Grain Almond-Raisin K cold 140 3 2 220 3.0 21.0 7 130 25 3 1.33 0.67 40.69232 208.95522
Nutri-grain Wheat K cold 90 3 0 170 3.0 18.0 2 90 25 3 1.00 1.00 59.64284 90.00000
Oatmeal Raisin Crisp G cold 130 3 2 170 1.5 13.5 10 120 25 3 1.25 0.50 30.45084 260.00000
Post Nat. Raisin Bran P cold 120 3 1 200 6.0 11.0 14 260 25 3 1.33 0.67 37.84059 179.10448
Product 19 K cold 100 3 0 320 1.0 20.0 3 45 100 3 1.00 1.00 41.50354 100.00000
Puffed Rice Q cold 50 1 0 0 0.0 13.0 0 15 0 3 0.50 1.00 60.75611 50.00000
Puffed Wheat Q cold 50 2 0 0 1.0 10.0 0 50 0 3 0.50 1.00 63.00565 50.00000
Quaker Oat Squares Q cold 100 4 1 135 2.0 14.0 6 110 25 3 1.00 0.50 49.51187 200.00000
Quaker Oatmeal Q hot 100 5 2 0 2.7 -1.0 -1 110 0 1 1.00 0.67 50.82839 149.25373
Raisin Bran K cold 120 3 1 210 5.0 14.0 12 240 25 2 1.33 0.75 39.25920 160.00000
Raisin Nut Bran G cold 100 3 2 140 2.5 10.5 8 140 25 3 1.00 0.50 39.70340 200.00000
Raisin Squares K cold 90 2 0 0 2.0 15.0 6 110 25 3 1.00 0.50 55.33314 180.00000
Rice Chex R cold 110 1 0 240 0.0 23.0 2 30 25 1 1.00 1.13 41.99893 97.34513
Rice Krispies K cold 110 2 0 290 0.0 22.0 3 35 25 1 1.00 1.00 40.56016 110.00000
Shredded Wheat N cold 80 2 0 0 3.0 16.0 0 95 0 1 0.83 1.00 68.23588 80.00000
Shredded Wheat 'n'Bran N cold 90 3 0 0 4.0 19.0 0 140 0 1 1.00 0.67 74.47295 134.32836
Shredded Wheat spoon size N cold 90 3 0 0 3.0 20.0 0 120 0 1 1.00 0.67 72.80179 134.32836
Smacks K cold 110 2 1 70 1.0 9.0 15 40 25 2 1.00 0.75 31.23005 146.66667
Special K K cold 110 6 0 230 1.0 16.0 3 55 25 1 1.00 1.00 53.13132 110.00000
Strawberry Fruit Wheats N cold 90 2 0 15 3.0 15.0 5 90 25 2 1.00 1.00 59.36399 90.00000
Total Corn Flakes G cold 110 2 1 200 0.0 21.0 3 35 100 3 1.00 1.00 38.83975 110.00000
Total Raisin Bran G cold 140 3 1 190 4.0 15.0 14 230 100 3 1.50 1.00 28.59278 140.00000
Total Whole Grain G cold 100 3 1 200 3.0 16.0 3 110 100 3 1.00 1.00 46.65884 100.00000
Triples G cold 110 2 1 250 0.0 21.0 3 60 25 3 1.00 0.75 39.10617 146.66667
Trix G cold 110 1 1 140 0.0 13.0 12 25 25 2 1.00 1.00 27.75330 110.00000
Wheat Chex R cold 100 3 1 230 3.0 17.0 3 115 25 1 1.00 0.67 49.78744 149.25373
Wheaties G cold 100 3 1 200 3.0 17.0 3 110 25 1 1.00 1.00 51.59219 100.00000
Wheaties Honey Gold G cold 110 2 1 200 1.0 16.0 8 60 25 1 1.00 0.75 36.18756 146.66667

OR revise an existing variable.

cereal |> 
  mutate(shelf = as.factor(shelf))

mutate(): Handy Helpers!

  • if_else() or case_when() – shortcut for if-else loop
  • as.factor(), as.numeric(), etc. – change variable type
  • +, -, *, / – basic mathematical operations
  • %% – modulo (returns the remainder when doing division)

How do we “mutate” in base R?

You can define new columns!

cereal$cal_per_cup <- cereal$calories / cereal$cups
name manuf type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating cal_per_cup
100% Bran N cold 70 4 1 130 10.0 5.0 6 280 25 3 1.00 0.33 68.40297 212.12121
100% Natural Bran Q cold 120 3 5 15 2.0 8.0 8 135 0 3 1.00 1.00 33.98368 120.00000
All-Bran K cold 70 4 1 260 9.0 7.0 5 320 25 3 1.00 0.33 59.42551 212.12121
All-Bran with Extra Fiber K cold 50 4 0 140 14.0 8.0 0 330 25 3 1.00 0.50 93.70491 100.00000
Almond Delight R cold 110 2 2 200 1.0 14.0 8 -1 25 3 1.00 0.75 34.38484 146.66667
Apple Cinnamon Cheerios G cold 110 2 2 180 1.5 10.5 10 70 25 1 1.00 0.75 29.50954 146.66667
Apple Jacks K cold 110 2 0 125 1.0 11.0 14 30 25 2 1.00 1.00 33.17409 110.00000
Basic 4 G cold 130 3 2 210 2.0 18.0 8 100 25 3 1.33 0.75 37.03856 173.33333
Bran Chex R cold 90 2 1 200 4.0 15.0 6 125 25 1 1.00 0.67 49.12025 134.32836
Bran Flakes P cold 90 3 0 210 5.0 13.0 5 190 25 3 1.00 0.67 53.31381 134.32836
Cap'n'Crunch Q cold 120 1 2 220 0.0 12.0 12 35 25 2 1.00 0.75 18.04285 160.00000
Cheerios G cold 110 6 2 290 2.0 17.0 1 105 25 1 1.00 1.25 50.76500 88.00000
Cinnamon Toast Crunch G cold 120 1 3 210 0.0 13.0 9 45 25 2 1.00 0.75 19.82357 160.00000
Clusters G cold 110 3 2 140 2.0 13.0 7 105 25 3 1.00 0.50 40.40021 220.00000
Cocoa Puffs G cold 110 1 1 180 0.0 12.0 13 55 25 2 1.00 1.00 22.73645 110.00000
Corn Chex R cold 110 2 0 280 0.0 22.0 3 25 25 1 1.00 1.00 41.44502 110.00000
Corn Flakes K cold 100 2 0 290 1.0 21.0 2 35 25 1 1.00 1.00 45.86332 100.00000
Corn Pops K cold 110 1 0 90 1.0 13.0 12 20 25 2 1.00 1.00 35.78279 110.00000
Count Chocula G cold 110 1 1 180 0.0 12.0 13 65 25 2 1.00 1.00 22.39651 110.00000
Cracklin' Oat Bran K cold 110 3 3 140 4.0 10.0 7 160 25 3 1.00 0.50 40.44877 220.00000
Cream of Wheat (Quick) N hot 100 3 0 80 1.0 21.0 0 -1 0 2 1.00 1.00 64.53382 100.00000
Crispix K cold 110 2 0 220 1.0 21.0 3 30 25 3 1.00 1.00 46.89564 110.00000
Crispy Wheat & Raisins G cold 100 2 1 140 2.0 11.0 10 120 25 3 1.00 0.75 36.17620 133.33333
Double Chex R cold 100 2 0 190 1.0 18.0 5 80 25 3 1.00 0.75 44.33086 133.33333
Froot Loops K cold 110 2 1 125 1.0 11.0 13 30 25 2 1.00 1.00 32.20758 110.00000
Frosted Flakes K cold 110 1 0 200 1.0 14.0 11 25 25 1 1.00 0.75 31.43597 146.66667
Frosted Mini-Wheats K cold 100 3 0 0 3.0 14.0 7 100 25 2 1.00 0.80 58.34514 125.00000
Fruit & Fibre Dates; Walnuts; and Oats P cold 120 3 2 160 5.0 12.0 10 200 25 3 1.25 0.67 40.91705 179.10448
Fruitful Bran K cold 120 3 0 240 5.0 14.0 12 190 25 3 1.33 0.67 41.01549 179.10448
Fruity Pebbles P cold 110 1 1 135 0.0 13.0 12 25 25 2 1.00 0.75 28.02576 146.66667
Golden Crisp P cold 100 2 0 45 0.0 11.0 15 40 25 1 1.00 0.88 35.25244 113.63636
Golden Grahams G cold 110 1 1 280 0.0 15.0 9 45 25 2 1.00 0.75 23.80404 146.66667
Grape Nuts Flakes P cold 100 3 1 140 3.0 15.0 5 85 25 3 1.00 0.88 52.07690 113.63636
Grape-Nuts P cold 110 3 0 170 3.0 17.0 3 90 25 3 1.00 0.25 53.37101 440.00000
Great Grains Pecan P cold 120 3 3 75 3.0 13.0 4 100 25 3 1.00 0.33 45.81172 363.63636
Honey Graham Ohs Q cold 120 1 2 220 1.0 12.0 11 45 25 2 1.00 1.00 21.87129 120.00000
Honey Nut Cheerios G cold 110 3 1 250 1.5 11.5 10 90 25 1 1.00 0.75 31.07222 146.66667
Honey-comb P cold 110 1 0 180 0.0 14.0 11 35 25 1 1.00 1.33 28.74241 82.70677
Just Right Crunchy Nuggets K cold 110 2 1 170 1.0 17.0 6 60 100 3 1.00 1.00 36.52368 110.00000
Just Right Fruit & Nut K cold 140 3 1 170 2.0 20.0 9 95 100 3 1.30 0.75 36.47151 186.66667
Kix G cold 110 2 1 260 0.0 21.0 3 40 25 2 1.00 1.50 39.24111 73.33333
Life Q cold 100 4 2 150 2.0 12.0 6 95 25 2 1.00 0.67 45.32807 149.25373
Lucky Charms G cold 110 2 1 180 0.0 12.0 12 55 25 2 1.00 1.00 26.73451 110.00000
Maypo A hot 100 4 1 0 0.0 16.0 3 95 25 2 1.00 1.00 54.85092 100.00000
Muesli Raisins; Dates; & Almonds R cold 150 4 3 95 3.0 16.0 11 170 25 3 1.00 1.00 37.13686 150.00000
Muesli Raisins; Peaches; & Pecans R cold 150 4 3 150 3.0 16.0 11 170 25 3 1.00 1.00 34.13976 150.00000
Mueslix Crispy Blend K cold 160 3 2 150 3.0 17.0 13 160 25 3 1.50 0.67 30.31335 238.80597
Multi-Grain Cheerios G cold 100 2 1 220 2.0 15.0 6 90 25 1 1.00 1.00 40.10596 100.00000
Nut&Honey Crunch K cold 120 2 1 190 0.0 15.0 9 40 25 2 1.00 0.67 29.92429 179.10448
Nutri-Grain Almond-Raisin K cold 140 3 2 220 3.0 21.0 7 130 25 3 1.33 0.67 40.69232 208.95522
Nutri-grain Wheat K cold 90 3 0 170 3.0 18.0 2 90 25 3 1.00 1.00 59.64284 90.00000
Oatmeal Raisin Crisp G cold 130 3 2 170 1.5 13.5 10 120 25 3 1.25 0.50 30.45084 260.00000
Post Nat. Raisin Bran P cold 120 3 1 200 6.0 11.0 14 260 25 3 1.33 0.67 37.84059 179.10448
Product 19 K cold 100 3 0 320 1.0 20.0 3 45 100 3 1.00 1.00 41.50354 100.00000
Puffed Rice Q cold 50 1 0 0 0.0 13.0 0 15 0 3 0.50 1.00 60.75611 50.00000
Puffed Wheat Q cold 50 2 0 0 1.0 10.0 0 50 0 3 0.50 1.00 63.00565 50.00000
Quaker Oat Squares Q cold 100 4 1 135 2.0 14.0 6 110 25 3 1.00 0.50 49.51187 200.00000
Quaker Oatmeal Q hot 100 5 2 0 2.7 -1.0 -1 110 0 1 1.00 0.67 50.82839 149.25373
Raisin Bran K cold 120 3 1 210 5.0 14.0 12 240 25 2 1.33 0.75 39.25920 160.00000
Raisin Nut Bran G cold 100 3 2 140 2.5 10.5 8 140 25 3 1.00 0.50 39.70340 200.00000
Raisin Squares K cold 90 2 0 0 2.0 15.0 6 110 25 3 1.00 0.50 55.33314 180.00000
Rice Chex R cold 110 1 0 240 0.0 23.0 2 30 25 1 1.00 1.13 41.99893 97.34513
Rice Krispies K cold 110 2 0 290 0.0 22.0 3 35 25 1 1.00 1.00 40.56016 110.00000
Shredded Wheat N cold 80 2 0 0 3.0 16.0 0 95 0 1 0.83 1.00 68.23588 80.00000
Shredded Wheat 'n'Bran N cold 90 3 0 0 4.0 19.0 0 140 0 1 1.00 0.67 74.47295 134.32836
Shredded Wheat spoon size N cold 90 3 0 0 3.0 20.0 0 120 0 1 1.00 0.67 72.80179 134.32836
Smacks K cold 110 2 1 70 1.0 9.0 15 40 25 2 1.00 0.75 31.23005 146.66667
Special K K cold 110 6 0 230 1.0 16.0 3 55 25 1 1.00 1.00 53.13132 110.00000
Strawberry Fruit Wheats N cold 90 2 0 15 3.0 15.0 5 90 25 2 1.00 1.00 59.36399 90.00000
Total Corn Flakes G cold 110 2 1 200 0.0 21.0 3 35 100 3 1.00 1.00 38.83975 110.00000
Total Raisin Bran G cold 140 3 1 190 4.0 15.0 14 230 100 3 1.50 1.00 28.59278 140.00000
Total Whole Grain G cold 100 3 1 200 3.0 16.0 3 110 100 3 1.00 1.00 46.65884 100.00000
Triples G cold 110 2 1 250 0.0 21.0 3 60 25 3 1.00 0.75 39.10617 146.66667
Trix G cold 110 1 1 140 0.0 13.0 12 25 25 2 1.00 1.00 27.75330 110.00000
Wheat Chex R cold 100 3 1 230 3.0 17.0 3 115 25 1 1.00 0.67 49.78744 149.25373
Wheaties G cold 100 3 1 200 3.0 17.0 3 110 25 1 1.00 1.00 51.59219 100.00000
Wheaties Honey Gold G cold 110 2 1 200 1.0 16.0 8 60 25 1 1.00 0.75 36.18756 146.66667

OR overwrite old ones!

cereal$shelf <- as.factor(cereal$shelf)

group_by()

The ungroup() command is just as important as the group_by() command! (by Allison Horst)

group_by()

Separate the data into different groups based on a categorical variable.

  • The data gets grouped, but nothing happens externally.
cereal |> 
  group_by(type)
# A tibble: 77 × 16
# Groups:   type [2]
   name      manuf type  calories protein   fat sodium fiber carbo sugars potass
   <fct>     <fct> <fct>    <int>   <int> <int>  <int> <dbl> <dbl>  <int>  <int>
 1 100% Bran N     cold        70       4     1    130  10     5        6    280
 2 100% Nat… Q     cold       120       3     5     15   2     8        8    135
 3 All-Bran  K     cold        70       4     1    260   9     7        5    320
 4 All-Bran… K     cold        50       4     0    140  14     8        0    330
 5 Almond D… R     cold       110       2     2    200   1    14        8     -1
 6 Apple Ci… G     cold       110       2     2    180   1.5  10.5     10     70
 7 Apple Ja… K     cold       110       2     0    125   1    11       14     30
 8 Basic 4   G     cold       130       3     2    210   2    18        8    100
 9 Bran Chex R     cold        90       2     1    200   4    15        6    125
10 Bran Fla… P     cold        90       3     0    210   5    13        5    190
# ℹ 67 more rows
# ℹ 5 more variables: vitamins <int>, shelf <int>, weight <dbl>, cups <dbl>,
#   rating <dbl>
name manuf type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating
100% Bran N cold 70 4 1 130 10.0 5.0 6 280 25 3 1.00 0.33 68.40297
100% Natural Bran Q cold 120 3 5 15 2.0 8.0 8 135 0 3 1.00 1.00 33.98368
All-Bran K cold 70 4 1 260 9.0 7.0 5 320 25 3 1.00 0.33 59.42551
All-Bran with Extra Fiber K cold 50 4 0 140 14.0 8.0 0 330 25 3 1.00 0.50 93.70491
Almond Delight R cold 110 2 2 200 1.0 14.0 8 -1 25 3 1.00 0.75 34.38484
Apple Cinnamon Cheerios G cold 110 2 2 180 1.5 10.5 10 70 25 1 1.00 0.75 29.50954
Apple Jacks K cold 110 2 0 125 1.0 11.0 14 30 25 2 1.00 1.00 33.17409
Basic 4 G cold 130 3 2 210 2.0 18.0 8 100 25 3 1.33 0.75 37.03856
Bran Chex R cold 90 2 1 200 4.0 15.0 6 125 25 1 1.00 0.67 49.12025
Bran Flakes P cold 90 3 0 210 5.0 13.0 5 190 25 3 1.00 0.67 53.31381
Cap'n'Crunch Q cold 120 1 2 220 0.0 12.0 12 35 25 2 1.00 0.75 18.04285
Cheerios G cold 110 6 2 290 2.0 17.0 1 105 25 1 1.00 1.25 50.76500
Cinnamon Toast Crunch G cold 120 1 3 210 0.0 13.0 9 45 25 2 1.00 0.75 19.82357
Clusters G cold 110 3 2 140 2.0 13.0 7 105 25 3 1.00 0.50 40.40021
Cocoa Puffs G cold 110 1 1 180 0.0 12.0 13 55 25 2 1.00 1.00 22.73645
Corn Chex R cold 110 2 0 280 0.0 22.0 3 25 25 1 1.00 1.00 41.44502
Corn Flakes K cold 100 2 0 290 1.0 21.0 2 35 25 1 1.00 1.00 45.86332
Corn Pops K cold 110 1 0 90 1.0 13.0 12 20 25 2 1.00 1.00 35.78279
Count Chocula G cold 110 1 1 180 0.0 12.0 13 65 25 2 1.00 1.00 22.39651
Cracklin' Oat Bran K cold 110 3 3 140 4.0 10.0 7 160 25 3 1.00 0.50 40.44877
Cream of Wheat (Quick) N hot 100 3 0 80 1.0 21.0 0 -1 0 2 1.00 1.00 64.53382
Crispix K cold 110 2 0 220 1.0 21.0 3 30 25 3 1.00 1.00 46.89564
Crispy Wheat & Raisins G cold 100 2 1 140 2.0 11.0 10 120 25 3 1.00 0.75 36.17620
Double Chex R cold 100 2 0 190 1.0 18.0 5 80 25 3 1.00 0.75 44.33086
Froot Loops K cold 110 2 1 125 1.0 11.0 13 30 25 2 1.00 1.00 32.20758
Frosted Flakes K cold 110 1 0 200 1.0 14.0 11 25 25 1 1.00 0.75 31.43597
Frosted Mini-Wheats K cold 100 3 0 0 3.0 14.0 7 100 25 2 1.00 0.80 58.34514
Fruit & Fibre Dates; Walnuts; and Oats P cold 120 3 2 160 5.0 12.0 10 200 25 3 1.25 0.67 40.91705
Fruitful Bran K cold 120 3 0 240 5.0 14.0 12 190 25 3 1.33 0.67 41.01549
Fruity Pebbles P cold 110 1 1 135 0.0 13.0 12 25 25 2 1.00 0.75 28.02576
Golden Crisp P cold 100 2 0 45 0.0 11.0 15 40 25 1 1.00 0.88 35.25244
Golden Grahams G cold 110 1 1 280 0.0 15.0 9 45 25 2 1.00 0.75 23.80404
Grape Nuts Flakes P cold 100 3 1 140 3.0 15.0 5 85 25 3 1.00 0.88 52.07690
Grape-Nuts P cold 110 3 0 170 3.0 17.0 3 90 25 3 1.00 0.25 53.37101
Great Grains Pecan P cold 120 3 3 75 3.0 13.0 4 100 25 3 1.00 0.33 45.81172
Honey Graham Ohs Q cold 120 1 2 220 1.0 12.0 11 45 25 2 1.00 1.00 21.87129
Honey Nut Cheerios G cold 110 3 1 250 1.5 11.5 10 90 25 1 1.00 0.75 31.07222
Honey-comb P cold 110 1 0 180 0.0 14.0 11 35 25 1 1.00 1.33 28.74241
Just Right Crunchy Nuggets K cold 110 2 1 170 1.0 17.0 6 60 100 3 1.00 1.00 36.52368
Just Right Fruit & Nut K cold 140 3 1 170 2.0 20.0 9 95 100 3 1.30 0.75 36.47151
Kix G cold 110 2 1 260 0.0 21.0 3 40 25 2 1.00 1.50 39.24111
Life Q cold 100 4 2 150 2.0 12.0 6 95 25 2 1.00 0.67 45.32807
Lucky Charms G cold 110 2 1 180 0.0 12.0 12 55 25 2 1.00 1.00 26.73451
Maypo A hot 100 4 1 0 0.0 16.0 3 95 25 2 1.00 1.00 54.85092
Muesli Raisins; Dates; & Almonds R cold 150 4 3 95 3.0 16.0 11 170 25 3 1.00 1.00 37.13686
Muesli Raisins; Peaches; & Pecans R cold 150 4 3 150 3.0 16.0 11 170 25 3 1.00 1.00 34.13976
Mueslix Crispy Blend K cold 160 3 2 150 3.0 17.0 13 160 25 3 1.50 0.67 30.31335
Multi-Grain Cheerios G cold 100 2 1 220 2.0 15.0 6 90 25 1 1.00 1.00 40.10596
Nut&Honey Crunch K cold 120 2 1 190 0.0 15.0 9 40 25 2 1.00 0.67 29.92429
Nutri-Grain Almond-Raisin K cold 140 3 2 220 3.0 21.0 7 130 25 3 1.33 0.67 40.69232
Nutri-grain Wheat K cold 90 3 0 170 3.0 18.0 2 90 25 3 1.00 1.00 59.64284
Oatmeal Raisin Crisp G cold 130 3 2 170 1.5 13.5 10 120 25 3 1.25 0.50 30.45084
Post Nat. Raisin Bran P cold 120 3 1 200 6.0 11.0 14 260 25 3 1.33 0.67 37.84059
Product 19 K cold 100 3 0 320 1.0 20.0 3 45 100 3 1.00 1.00 41.50354
Puffed Rice Q cold 50 1 0 0 0.0 13.0 0 15 0 3 0.50 1.00 60.75611
Puffed Wheat Q cold 50 2 0 0 1.0 10.0 0 50 0 3 0.50 1.00 63.00565
Quaker Oat Squares Q cold 100 4 1 135 2.0 14.0 6 110 25 3 1.00 0.50 49.51187
Quaker Oatmeal Q hot 100 5 2 0 2.7 -1.0 -1 110 0 1 1.00 0.67 50.82839
Raisin Bran K cold 120 3 1 210 5.0 14.0 12 240 25 2 1.33 0.75 39.25920
Raisin Nut Bran G cold 100 3 2 140 2.5 10.5 8 140 25 3 1.00 0.50 39.70340
Raisin Squares K cold 90 2 0 0 2.0 15.0 6 110 25 3 1.00 0.50 55.33314
Rice Chex R cold 110 1 0 240 0.0 23.0 2 30 25 1 1.00 1.13 41.99893
Rice Krispies K cold 110 2 0 290 0.0 22.0 3 35 25 1 1.00 1.00 40.56016
Shredded Wheat N cold 80 2 0 0 3.0 16.0 0 95 0 1 0.83 1.00 68.23588
Shredded Wheat 'n'Bran N cold 90 3 0 0 4.0 19.0 0 140 0 1 1.00 0.67 74.47295
Shredded Wheat spoon size N cold 90 3 0 0 3.0 20.0 0 120 0 1 1.00 0.67 72.80179
Smacks K cold 110 2 1 70 1.0 9.0 15 40 25 2 1.00 0.75 31.23005
Special K K cold 110 6 0 230 1.0 16.0 3 55 25 1 1.00 1.00 53.13132
Strawberry Fruit Wheats N cold 90 2 0 15 3.0 15.0 5 90 25 2 1.00 1.00 59.36399
Total Corn Flakes G cold 110 2 1 200 0.0 21.0 3 35 100 3 1.00 1.00 38.83975
Total Raisin Bran G cold 140 3 1 190 4.0 15.0 14 230 100 3 1.50 1.00 28.59278
Total Whole Grain G cold 100 3 1 200 3.0 16.0 3 110 100 3 1.00 1.00 46.65884
Triples G cold 110 2 1 250 0.0 21.0 3 60 25 3 1.00 0.75 39.10617
Trix G cold 110 1 1 140 0.0 13.0 12 25 25 2 1.00 1.00 27.75330
Wheat Chex R cold 100 3 1 230 3.0 17.0 3 115 25 1 1.00 0.67 49.78744
Wheaties G cold 100 3 1 200 3.0 17.0 3 110 25 1 1.00 1.00 51.59219
Wheaties Honey Gold G cold 110 2 1 200 1.0 16.0 8 60 25 1 1.00 0.75 36.18756

ungroup()

The ungroup() function will remove the internal grouping in your data.

  • This is not something that you typically need to do, but if you are getting weird errors downstream from a group_by() statement, try ungrouping your data!

summarize()

group_by() is almost always paired with summarize()!

summarize()

We can calculate summaries of variables in the data.

cereal |> 
  summarise(mean_calories = mean(calories))
  mean_calories
1      106.8831

Or multiple summaries at the same time.

cereal |> 
summarise(mean_calories = mean(calories),
          num_cereals = n(),
          mean_sugar = mean(sugars))
  mean_calories num_cereals mean_sugar
1      106.8831          77   6.922078

Note

summarize() and summarise() are synonyms!

summarize(): Handy Helpers!

  • mean(), median(), sd(), sum()
  • min(), max()
  • n(), n_distinct() – counts the number of (distinct) elements
  • first(), last(), nth() – extract the first, last, or nth element
  • across() – apply a function across columns

group_by() + summarize()!

  1. goup_by a variable (or multiple variables)
  2. summarize a variable (or multiple variables) within the groups
cereal |> 
  group_by(manuf) |> 
  summarise(mean_calories = mean(calories))
manuf mean_calories
A 100.00000
G 111.36364
K 108.69565
N 86.66667
P 108.88889
Q 95.00000
R 115.00000

group_by() + mutate()!

  1. goup_by a variable (or multiple variables)
  2. mutate a variable (or multiple variables) within the groups
cereal |> 
  group_by(manuf) |> 
  mutate(mean_calories = mean(calories))
name manuf type calories protein fat sodium fiber carbo sugars potass vitamins shelf weight cups rating mean_calories
100% Bran N cold 70 4 1 130 10.0 5.0 6 280 25 3 1.00 0.33 68.40297 86.66667
100% Natural Bran Q cold 120 3 5 15 2.0 8.0 8 135 0 3 1.00 1.00 33.98368 95.00000
All-Bran K cold 70 4 1 260 9.0 7.0 5 320 25 3 1.00 0.33 59.42551 108.69565
All-Bran with Extra Fiber K cold 50 4 0 140 14.0 8.0 0 330 25 3 1.00 0.50 93.70491 108.69565
Almond Delight R cold 110 2 2 200 1.0 14.0 8 -1 25 3 1.00 0.75 34.38484 115.00000
Apple Cinnamon Cheerios G cold 110 2 2 180 1.5 10.5 10 70 25 1 1.00 0.75 29.50954 111.36364
Apple Jacks K cold 110 2 0 125 1.0 11.0 14 30 25 2 1.00 1.00 33.17409 108.69565
Basic 4 G cold 130 3 2 210 2.0 18.0 8 100 25 3 1.33 0.75 37.03856 111.36364
Bran Chex R cold 90 2 1 200 4.0 15.0 6 125 25 1 1.00 0.67 49.12025 115.00000
Bran Flakes P cold 90 3 0 210 5.0 13.0 5 190 25 3 1.00 0.67 53.31381 108.88889
Cap'n'Crunch Q cold 120 1 2 220 0.0 12.0 12 35 25 2 1.00 0.75 18.04285 95.00000
Cheerios G cold 110 6 2 290 2.0 17.0 1 105 25 1 1.00 1.25 50.76500 111.36364
Cinnamon Toast Crunch G cold 120 1 3 210 0.0 13.0 9 45 25 2 1.00 0.75 19.82357 111.36364
Clusters G cold 110 3 2 140 2.0 13.0 7 105 25 3 1.00 0.50 40.40021 111.36364
Cocoa Puffs G cold 110 1 1 180 0.0 12.0 13 55 25 2 1.00 1.00 22.73645 111.36364
Corn Chex R cold 110 2 0 280 0.0 22.0 3 25 25 1 1.00 1.00 41.44502 115.00000
Corn Flakes K cold 100 2 0 290 1.0 21.0 2 35 25 1 1.00 1.00 45.86332 108.69565
Corn Pops K cold 110 1 0 90 1.0 13.0 12 20 25 2 1.00 1.00 35.78279 108.69565
Count Chocula G cold 110 1 1 180 0.0 12.0 13 65 25 2 1.00 1.00 22.39651 111.36364
Cracklin' Oat Bran K cold 110 3 3 140 4.0 10.0 7 160 25 3 1.00 0.50 40.44877 108.69565
Cream of Wheat (Quick) N hot 100 3 0 80 1.0 21.0 0 -1 0 2 1.00 1.00 64.53382 86.66667
Crispix K cold 110 2 0 220 1.0 21.0 3 30 25 3 1.00 1.00 46.89564 108.69565
Crispy Wheat & Raisins G cold 100 2 1 140 2.0 11.0 10 120 25 3 1.00 0.75 36.17620 111.36364
Double Chex R cold 100 2 0 190 1.0 18.0 5 80 25 3 1.00 0.75 44.33086 115.00000
Froot Loops K cold 110 2 1 125 1.0 11.0 13 30 25 2 1.00 1.00 32.20758 108.69565
Frosted Flakes K cold 110 1 0 200 1.0 14.0 11 25 25 1 1.00 0.75 31.43597 108.69565
Frosted Mini-Wheats K cold 100 3 0 0 3.0 14.0 7 100 25 2 1.00 0.80 58.34514 108.69565
Fruit & Fibre Dates; Walnuts; and Oats P cold 120 3 2 160 5.0 12.0 10 200 25 3 1.25 0.67 40.91705 108.88889
Fruitful Bran K cold 120 3 0 240 5.0 14.0 12 190 25 3 1.33 0.67 41.01549 108.69565
Fruity Pebbles P cold 110 1 1 135 0.0 13.0 12 25 25 2 1.00 0.75 28.02576 108.88889
Golden Crisp P cold 100 2 0 45 0.0 11.0 15 40 25 1 1.00 0.88 35.25244 108.88889
Golden Grahams G cold 110 1 1 280 0.0 15.0 9 45 25 2 1.00 0.75 23.80404 111.36364
Grape Nuts Flakes P cold 100 3 1 140 3.0 15.0 5 85 25 3 1.00 0.88 52.07690 108.88889
Grape-Nuts P cold 110 3 0 170 3.0 17.0 3 90 25 3 1.00 0.25 53.37101 108.88889
Great Grains Pecan P cold 120 3 3 75 3.0 13.0 4 100 25 3 1.00 0.33 45.81172 108.88889
Honey Graham Ohs Q cold 120 1 2 220 1.0 12.0 11 45 25 2 1.00 1.00 21.87129 95.00000
Honey Nut Cheerios G cold 110 3 1 250 1.5 11.5 10 90 25 1 1.00 0.75 31.07222 111.36364
Honey-comb P cold 110 1 0 180 0.0 14.0 11 35 25 1 1.00 1.33 28.74241 108.88889
Just Right Crunchy Nuggets K cold 110 2 1 170 1.0 17.0 6 60 100 3 1.00 1.00 36.52368 108.69565
Just Right Fruit & Nut K cold 140 3 1 170 2.0 20.0 9 95 100 3 1.30 0.75 36.47151 108.69565
Kix G cold 110 2 1 260 0.0 21.0 3 40 25 2 1.00 1.50 39.24111 111.36364
Life Q cold 100 4 2 150 2.0 12.0 6 95 25 2 1.00 0.67 45.32807 95.00000
Lucky Charms G cold 110 2 1 180 0.0 12.0 12 55 25 2 1.00 1.00 26.73451 111.36364
Maypo A hot 100 4 1 0 0.0 16.0 3 95 25 2 1.00 1.00 54.85092 100.00000
Muesli Raisins; Dates; & Almonds R cold 150 4 3 95 3.0 16.0 11 170 25 3 1.00 1.00 37.13686 115.00000
Muesli Raisins; Peaches; & Pecans R cold 150 4 3 150 3.0 16.0 11 170 25 3 1.00 1.00 34.13976 115.00000
Mueslix Crispy Blend K cold 160 3 2 150 3.0 17.0 13 160 25 3 1.50 0.67 30.31335 108.69565
Multi-Grain Cheerios G cold 100 2 1 220 2.0 15.0 6 90 25 1 1.00 1.00 40.10596 111.36364
Nut&Honey Crunch K cold 120 2 1 190 0.0 15.0 9 40 25 2 1.00 0.67 29.92429 108.69565
Nutri-Grain Almond-Raisin K cold 140 3 2 220 3.0 21.0 7 130 25 3 1.33 0.67 40.69232 108.69565
Nutri-grain Wheat K cold 90 3 0 170 3.0 18.0 2 90 25 3 1.00 1.00 59.64284 108.69565
Oatmeal Raisin Crisp G cold 130 3 2 170 1.5 13.5 10 120 25 3 1.25 0.50 30.45084 111.36364
Post Nat. Raisin Bran P cold 120 3 1 200 6.0 11.0 14 260 25 3 1.33 0.67 37.84059 108.88889
Product 19 K cold 100 3 0 320 1.0 20.0 3 45 100 3 1.00 1.00 41.50354 108.69565
Puffed Rice Q cold 50 1 0 0 0.0 13.0 0 15 0 3 0.50 1.00 60.75611 95.00000
Puffed Wheat Q cold 50 2 0 0 1.0 10.0 0 50 0 3 0.50 1.00 63.00565 95.00000
Quaker Oat Squares Q cold 100 4 1 135 2.0 14.0 6 110 25 3 1.00 0.50 49.51187 95.00000
Quaker Oatmeal Q hot 100 5 2 0 2.7 -1.0 -1 110 0 1 1.00 0.67 50.82839 95.00000
Raisin Bran K cold 120 3 1 210 5.0 14.0 12 240 25 2 1.33 0.75 39.25920 108.69565
Raisin Nut Bran G cold 100 3 2 140 2.5 10.5 8 140 25 3 1.00 0.50 39.70340 111.36364
Raisin Squares K cold 90 2 0 0 2.0 15.0 6 110 25 3 1.00 0.50 55.33314 108.69565
Rice Chex R cold 110 1 0 240 0.0 23.0 2 30 25 1 1.00 1.13 41.99893 115.00000
Rice Krispies K cold 110 2 0 290 0.0 22.0 3 35 25 1 1.00 1.00 40.56016 108.69565
Shredded Wheat N cold 80 2 0 0 3.0 16.0 0 95 0 1 0.83 1.00 68.23588 86.66667
Shredded Wheat 'n'Bran N cold 90 3 0 0 4.0 19.0 0 140 0 1 1.00 0.67 74.47295 86.66667
Shredded Wheat spoon size N cold 90 3 0 0 3.0 20.0 0 120 0 1 1.00 0.67 72.80179 86.66667
Smacks K cold 110 2 1 70 1.0 9.0 15 40 25 2 1.00 0.75 31.23005 108.69565
Special K K cold 110 6 0 230 1.0 16.0 3 55 25 1 1.00 1.00 53.13132 108.69565
Strawberry Fruit Wheats N cold 90 2 0 15 3.0 15.0 5 90 25 2 1.00 1.00 59.36399 86.66667
Total Corn Flakes G cold 110 2 1 200 0.0 21.0 3 35 100 3 1.00 1.00 38.83975 111.36364
Total Raisin Bran G cold 140 3 1 190 4.0 15.0 14 230 100 3 1.50 1.00 28.59278 111.36364
Total Whole Grain G cold 100 3 1 200 3.0 16.0 3 110 100 3 1.00 1.00 46.65884 111.36364
Triples G cold 110 2 1 250 0.0 21.0 3 60 25 3 1.00 0.75 39.10617 111.36364
Trix G cold 110 1 1 140 0.0 13.0 12 25 25 2 1.00 1.00 27.75330 111.36364
Wheat Chex R cold 100 3 1 230 3.0 17.0 3 115 25 1 1.00 0.67 49.78744 115.00000
Wheaties G cold 100 3 1 200 3.0 17.0 3 110 25 1 1.00 1.00 51.59219 111.36364
Wheaties Honey Gold G cold 110 2 1 200 1.0 16.0 8 60 25 1 1.00 0.75 36.18756 111.36364

Glue it all together!

cereal |> 
  filter(type == "cold") |> 
  mutate(cal_per_cup = calories / cups) |> 
  group_by(manuf) |> 
  summarise(mean_cal_per_cup = mean(cal_per_cup))
manuf mean_cal_per_cup
G 137.7879
K 145.3518
N 130.1556
P 194.7578
Q 121.3220
R 133.8659

Save your changes!

When you manipulate your data, make sure you assign your new dataset to a variable.

cereal_summary <- cereal |> 
  filter(type == "cold") |> 
  mutate(cal_per_cup = calories / cups) |> 
  group_by(manuf) |> 
  summarise(mean_cal_per_cup = mean(cal_per_cup))

Code Formatting

Similar to ggplot formatting with +, do not continue a line after writing a |>!

cereal |> group_by(type) |> summarise(mean_calories = mean(calories), num_cereals = n(), mean_sugar = mean(sugars))
cereal |> 
  group_by(type) |> 
  summarise(mean_calories = mean(calories), 
            num_cereals = n(),
            mean_sugar = mean(sugars))

PA 3: Identify the Mystery College

Today you will use the dplyr package to clean some data and then use that cleaned data to figure out what college Ephelia has been accepted to.

Submit the full name of the college Ephelia will attend to the Canvas Quiz.

To do…

  • PA 3: Identify the Mystery College
    • Due Wednesday 1/25 at 8:00am

Wednesday, April 19

Today we will…

  • Review PA 3: Identify the Mystery College
  • Extending dplyr verbs
    • across()
    • if_else()
    • case_when()
  • Implication of Data Ethics
  • Lab 3: Hip Hop Lyrics
  • Challenge 3: Published Comparisons & Data Ethics

Extending dplyr verbs

Example Data set – Cereal

library(liver)
data(cereal)
str(cereal, give.attr = FALSE)
'data.frame':   77 obs. of  16 variables:
 $ name    : Factor w/ 77 levels "100% Bran","100% Natural Bran",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ manuf   : Factor w/ 7 levels "A","G","K","N",..: 4 6 3 3 7 2 3 2 7 5 ...
 $ type    : Factor w/ 2 levels "cold","hot": 1 1 1 1 1 1 1 1 1 1 ...
 $ calories: int  70 120 70 50 110 110 110 130 90 90 ...
 $ protein : int  4 3 4 4 2 2 2 3 2 3 ...
 $ fat     : int  1 5 1 0 2 2 0 2 1 0 ...
 $ sodium  : int  130 15 260 140 200 180 125 210 200 210 ...
 $ fiber   : num  10 2 9 14 1 1.5 1 2 4 5 ...
 $ carbo   : num  5 8 7 8 14 10.5 11 18 15 13 ...
 $ sugars  : int  6 8 5 0 8 10 14 8 6 5 ...
 $ potass  : int  280 135 320 330 -1 70 30 100 125 190 ...
 $ vitamins: int  25 0 25 25 25 25 25 25 25 25 ...
 $ shelf   : int  3 3 3 3 3 1 2 3 1 3 ...
 $ weight  : num  1 1 1 1 1 1 1 1.33 1 1 ...
 $ cups    : num  0.33 1 0.33 0.5 0.75 0.75 1 0.75 0.67 0.67 ...
 $ rating  : num  68.4 34 59.4 93.7 34.4 ...
head(cereal)
                       name manuf type calories protein fat sodium fiber carbo
1                 100% Bran     N cold       70       4   1    130  10.0   5.0
2         100% Natural Bran     Q cold      120       3   5     15   2.0   8.0
3                  All-Bran     K cold       70       4   1    260   9.0   7.0
4 All-Bran with Extra Fiber     K cold       50       4   0    140  14.0   8.0
5            Almond Delight     R cold      110       2   2    200   1.0  14.0
6   Apple Cinnamon Cheerios     G cold      110       2   2    180   1.5  10.5
  sugars potass vitamins shelf weight cups   rating
1      6    280       25     3      1 0.33 68.40297
2      8    135        0     3      1 1.00 33.98368
3      5    320       25     3      1 0.33 59.42551
4      0    330       25     3      1 0.50 93.70491
5      8     -1       25     3      1 0.75 34.38484
6     10     70       25     1      1 0.75 29.50954
summary(cereal)
                        name    manuf    type       calories    
 100% Bran                : 1   A: 1   cold:74   Min.   : 50.0  
 100% Natural Bran        : 1   G:22   hot : 3   1st Qu.:100.0  
 All-Bran                 : 1   K:23             Median :110.0  
 All-Bran with Extra Fiber: 1   N: 6             Mean   :106.9  
 Almond Delight           : 1   P: 9             3rd Qu.:110.0  
 Apple Cinnamon Cheerios  : 1   Q: 8             Max.   :160.0  
 (Other)                  :71   R: 8                            
    protein           fat            sodium          fiber       
 Min.   :1.000   Min.   :0.000   Min.   :  0.0   Min.   : 0.000  
 1st Qu.:2.000   1st Qu.:0.000   1st Qu.:130.0   1st Qu.: 1.000  
 Median :3.000   Median :1.000   Median :180.0   Median : 2.000  
 Mean   :2.545   Mean   :1.013   Mean   :159.7   Mean   : 2.152  
 3rd Qu.:3.000   3rd Qu.:2.000   3rd Qu.:210.0   3rd Qu.: 3.000  
 Max.   :6.000   Max.   :5.000   Max.   :320.0   Max.   :14.000  
                                                                 
     carbo          sugars           potass          vitamins     
 Min.   :-1.0   Min.   :-1.000   Min.   : -1.00   Min.   :  0.00  
 1st Qu.:12.0   1st Qu.: 3.000   1st Qu.: 40.00   1st Qu.: 25.00  
 Median :14.0   Median : 7.000   Median : 90.00   Median : 25.00  
 Mean   :14.6   Mean   : 6.922   Mean   : 96.08   Mean   : 28.25  
 3rd Qu.:17.0   3rd Qu.:11.000   3rd Qu.:120.00   3rd Qu.: 25.00  
 Max.   :23.0   Max.   :15.000   Max.   :330.00   Max.   :100.00  
                                                                  
     shelf           weight          cups           rating     
 Min.   :1.000   Min.   :0.50   Min.   :0.250   Min.   :18.04  
 1st Qu.:1.000   1st Qu.:1.00   1st Qu.:0.670   1st Qu.:33.17  
 Median :2.000   Median :1.00   Median :0.750   Median :40.40  
 Mean   :2.208   Mean   :1.03   Mean   :0.821   Mean   :42.67  
 3rd Qu.:3.000   3rd Qu.:1.00   3rd Qu.:1.000   3rd Qu.:50.83  
 Max.   :3.000   Max.   :1.50   Max.   :1.500   Max.   :93.70  
                                                               

Summarize or mutate multiple columns with across()

cereal |> 
  group_by(type) |> 
  summarise(across(.cols = calories:potass, .fns = mean))
# A tibble: 2 × 9
  type  calories protein   fat sodium fiber carbo sugars potass
  <fct>    <dbl>   <dbl> <dbl>  <dbl> <dbl> <dbl>  <dbl>  <dbl>
1 cold      107.    2.49  1.01  165.   2.19  14.7  7.18    97.2
2 hot       100     4     1      26.7  1.23  12    0.667   68  

Discritize with if_else()

cereal |> 
  mutate(cal_cateory = if_else(calories < 100, "low", "high"), .after = calories)
                                     name manuf type calories cal_cateory
1                               100% Bran     N cold       70         low
2                       100% Natural Bran     Q cold      120        high
3                                All-Bran     K cold       70         low
4               All-Bran with Extra Fiber     K cold       50         low
5                          Almond Delight     R cold      110        high
6                 Apple Cinnamon Cheerios     G cold      110        high
7                             Apple Jacks     K cold      110        high
8                                 Basic 4     G cold      130        high
9                               Bran Chex     R cold       90         low
10                            Bran Flakes     P cold       90         low
11                           Cap'n'Crunch     Q cold      120        high
12                               Cheerios     G cold      110        high
13                  Cinnamon Toast Crunch     G cold      120        high
14                               Clusters     G cold      110        high
15                            Cocoa Puffs     G cold      110        high
16                              Corn Chex     R cold      110        high
17                            Corn Flakes     K cold      100        high
18                              Corn Pops     K cold      110        high
19                          Count Chocula     G cold      110        high
20                     Cracklin' Oat Bran     K cold      110        high
21                 Cream of Wheat (Quick)     N  hot      100        high
22                                Crispix     K cold      110        high
23                 Crispy Wheat & Raisins     G cold      100        high
24                            Double Chex     R cold      100        high
25                            Froot Loops     K cold      110        high
26                         Frosted Flakes     K cold      110        high
27                    Frosted Mini-Wheats     K cold      100        high
28 Fruit & Fibre Dates; Walnuts; and Oats     P cold      120        high
29                          Fruitful Bran     K cold      120        high
30                         Fruity Pebbles     P cold      110        high
31                           Golden Crisp     P cold      100        high
32                         Golden Grahams     G cold      110        high
33                      Grape Nuts Flakes     P cold      100        high
34                             Grape-Nuts     P cold      110        high
35                     Great Grains Pecan     P cold      120        high
36                       Honey Graham Ohs     Q cold      120        high
37                     Honey Nut Cheerios     G cold      110        high
38                             Honey-comb     P cold      110        high
39            Just Right Crunchy  Nuggets     K cold      110        high
40                 Just Right Fruit & Nut     K cold      140        high
41                                    Kix     G cold      110        high
42                                   Life     Q cold      100        high
43                           Lucky Charms     G cold      110        high
44                                  Maypo     A  hot      100        high
45       Muesli Raisins; Dates; & Almonds     R cold      150        high
46      Muesli Raisins; Peaches; & Pecans     R cold      150        high
47                   Mueslix Crispy Blend     K cold      160        high
48                   Multi-Grain Cheerios     G cold      100        high
49                       Nut&Honey Crunch     K cold      120        high
50              Nutri-Grain Almond-Raisin     K cold      140        high
51                      Nutri-grain Wheat     K cold       90         low
52                   Oatmeal Raisin Crisp     G cold      130        high
53                  Post Nat. Raisin Bran     P cold      120        high
54                             Product 19     K cold      100        high
55                            Puffed Rice     Q cold       50         low
56                           Puffed Wheat     Q cold       50         low
57                     Quaker Oat Squares     Q cold      100        high
58                         Quaker Oatmeal     Q  hot      100        high
59                            Raisin Bran     K cold      120        high
60                        Raisin Nut Bran     G cold      100        high
61                         Raisin Squares     K cold       90         low
62                              Rice Chex     R cold      110        high
63                          Rice Krispies     K cold      110        high
64                         Shredded Wheat     N cold       80         low
65                 Shredded Wheat 'n'Bran     N cold       90         low
66              Shredded Wheat spoon size     N cold       90         low
67                                 Smacks     K cold      110        high
68                              Special K     K cold      110        high
69                Strawberry Fruit Wheats     N cold       90         low
70                      Total Corn Flakes     G cold      110        high
71                      Total Raisin Bran     G cold      140        high
72                      Total Whole Grain     G cold      100        high
73                                Triples     G cold      110        high
74                                   Trix     G cold      110        high
75                             Wheat Chex     R cold      100        high
76                               Wheaties     G cold      100        high
77                    Wheaties Honey Gold     G cold      110        high
   protein fat sodium fiber carbo sugars potass vitamins shelf weight cups
1        4   1    130  10.0   5.0      6    280       25     3   1.00 0.33
2        3   5     15   2.0   8.0      8    135        0     3   1.00 1.00
3        4   1    260   9.0   7.0      5    320       25     3   1.00 0.33
4        4   0    140  14.0   8.0      0    330       25     3   1.00 0.50
5        2   2    200   1.0  14.0      8     -1       25     3   1.00 0.75
6        2   2    180   1.5  10.5     10     70       25     1   1.00 0.75
7        2   0    125   1.0  11.0     14     30       25     2   1.00 1.00
8        3   2    210   2.0  18.0      8    100       25     3   1.33 0.75
9        2   1    200   4.0  15.0      6    125       25     1   1.00 0.67
10       3   0    210   5.0  13.0      5    190       25     3   1.00 0.67
11       1   2    220   0.0  12.0     12     35       25     2   1.00 0.75
12       6   2    290   2.0  17.0      1    105       25     1   1.00 1.25
13       1   3    210   0.0  13.0      9     45       25     2   1.00 0.75
14       3   2    140   2.0  13.0      7    105       25     3   1.00 0.50
15       1   1    180   0.0  12.0     13     55       25     2   1.00 1.00
16       2   0    280   0.0  22.0      3     25       25     1   1.00 1.00
17       2   0    290   1.0  21.0      2     35       25     1   1.00 1.00
18       1   0     90   1.0  13.0     12     20       25     2   1.00 1.00
19       1   1    180   0.0  12.0     13     65       25     2   1.00 1.00
20       3   3    140   4.0  10.0      7    160       25     3   1.00 0.50
21       3   0     80   1.0  21.0      0     -1        0     2   1.00 1.00
22       2   0    220   1.0  21.0      3     30       25     3   1.00 1.00
23       2   1    140   2.0  11.0     10    120       25     3   1.00 0.75
24       2   0    190   1.0  18.0      5     80       25     3   1.00 0.75
25       2   1    125   1.0  11.0     13     30       25     2   1.00 1.00
26       1   0    200   1.0  14.0     11     25       25     1   1.00 0.75
27       3   0      0   3.0  14.0      7    100       25     2   1.00 0.80
28       3   2    160   5.0  12.0     10    200       25     3   1.25 0.67
29       3   0    240   5.0  14.0     12    190       25     3   1.33 0.67
30       1   1    135   0.0  13.0     12     25       25     2   1.00 0.75
31       2   0     45   0.0  11.0     15     40       25     1   1.00 0.88
32       1   1    280   0.0  15.0      9     45       25     2   1.00 0.75
33       3   1    140   3.0  15.0      5     85       25     3   1.00 0.88
34       3   0    170   3.0  17.0      3     90       25     3   1.00 0.25
35       3   3     75   3.0  13.0      4    100       25     3   1.00 0.33
36       1   2    220   1.0  12.0     11     45       25     2   1.00 1.00
37       3   1    250   1.5  11.5     10     90       25     1   1.00 0.75
38       1   0    180   0.0  14.0     11     35       25     1   1.00 1.33
39       2   1    170   1.0  17.0      6     60      100     3   1.00 1.00
40       3   1    170   2.0  20.0      9     95      100     3   1.30 0.75
41       2   1    260   0.0  21.0      3     40       25     2   1.00 1.50
42       4   2    150   2.0  12.0      6     95       25     2   1.00 0.67
43       2   1    180   0.0  12.0     12     55       25     2   1.00 1.00
44       4   1      0   0.0  16.0      3     95       25     2   1.00 1.00
45       4   3     95   3.0  16.0     11    170       25     3   1.00 1.00
46       4   3    150   3.0  16.0     11    170       25     3   1.00 1.00
47       3   2    150   3.0  17.0     13    160       25     3   1.50 0.67
48       2   1    220   2.0  15.0      6     90       25     1   1.00 1.00
49       2   1    190   0.0  15.0      9     40       25     2   1.00 0.67
50       3   2    220   3.0  21.0      7    130       25     3   1.33 0.67
51       3   0    170   3.0  18.0      2     90       25     3   1.00 1.00
52       3   2    170   1.5  13.5     10    120       25     3   1.25 0.50
53       3   1    200   6.0  11.0     14    260       25     3   1.33 0.67
54       3   0    320   1.0  20.0      3     45      100     3   1.00 1.00
55       1   0      0   0.0  13.0      0     15        0     3   0.50 1.00
56       2   0      0   1.0  10.0      0     50        0     3   0.50 1.00
57       4   1    135   2.0  14.0      6    110       25     3   1.00 0.50
58       5   2      0   2.7  -1.0     -1    110        0     1   1.00 0.67
59       3   1    210   5.0  14.0     12    240       25     2   1.33 0.75
60       3   2    140   2.5  10.5      8    140       25     3   1.00 0.50
61       2   0      0   2.0  15.0      6    110       25     3   1.00 0.50
62       1   0    240   0.0  23.0      2     30       25     1   1.00 1.13
63       2   0    290   0.0  22.0      3     35       25     1   1.00 1.00
64       2   0      0   3.0  16.0      0     95        0     1   0.83 1.00
65       3   0      0   4.0  19.0      0    140        0     1   1.00 0.67
66       3   0      0   3.0  20.0      0    120        0     1   1.00 0.67
67       2   1     70   1.0   9.0     15     40       25     2   1.00 0.75
68       6   0    230   1.0  16.0      3     55       25     1   1.00 1.00
69       2   0     15   3.0  15.0      5     90       25     2   1.00 1.00
70       2   1    200   0.0  21.0      3     35      100     3   1.00 1.00
71       3   1    190   4.0  15.0     14    230      100     3   1.50 1.00
72       3   1    200   3.0  16.0      3    110      100     3   1.00 1.00
73       2   1    250   0.0  21.0      3     60       25     3   1.00 0.75
74       1   1    140   0.0  13.0     12     25       25     2   1.00 1.00
75       3   1    230   3.0  17.0      3    115       25     1   1.00 0.67
76       3   1    200   3.0  17.0      3    110       25     1   1.00 1.00
77       2   1    200   1.0  16.0      8     60       25     1   1.00 0.75
     rating
1  68.40297
2  33.98368
3  59.42551
4  93.70491
5  34.38484
6  29.50954
7  33.17409
8  37.03856
9  49.12025
10 53.31381
11 18.04285
12 50.76500
13 19.82357
14 40.40021
15 22.73645
16 41.44502
17 45.86332
18 35.78279
19 22.39651
20 40.44877
21 64.53382
22 46.89564
23 36.17620
24 44.33086
25 32.20758
26 31.43597
27 58.34514
28 40.91705
29 41.01549
30 28.02576
31 35.25244
32 23.80404
33 52.07690
34 53.37101
35 45.81172
36 21.87129
37 31.07222
38 28.74241
39 36.52368
40 36.47151
41 39.24111
42 45.32807
43 26.73451
44 54.85092
45 37.13686
46 34.13976
47 30.31335
48 40.10596
49 29.92429
50 40.69232
51 59.64284
52 30.45084
53 37.84059
54 41.50354
55 60.75611
56 63.00565
57 49.51187
58 50.82839
59 39.25920
60 39.70340
61 55.33314
62 41.99893
63 40.56016
64 68.23588
65 74.47295
66 72.80179
67 31.23005
68 53.13132
69 59.36399
70 38.83975
71 28.59278
72 46.65884
73 39.10617
74 27.75330
75 49.78744
76 51.59219
77 36.18756

Re-leveling with case_when()

cereal |> 
  mutate(manuf = case_when(manuf == "A" ~ "American Home Food Products", 
                           manuf == "G" ~ "General Mills", 
                           manuf == "K" ~ "Kelloggs", 
                           manuf == "N" ~ "Nabisco", 
                           manuf == "P" ~ "Post", 
                           manuf == "Q" ~ "Quaker Oats", 
                           manuf == "R" ~ "Ralston Purina"
                           )
  )
                                     name                       manuf type
1                               100% Bran                     Nabisco cold
2                       100% Natural Bran                 Quaker Oats cold
3                                All-Bran                    Kelloggs cold
4               All-Bran with Extra Fiber                    Kelloggs cold
5                          Almond Delight              Ralston Purina cold
6                 Apple Cinnamon Cheerios               General Mills cold
7                             Apple Jacks                    Kelloggs cold
8                                 Basic 4               General Mills cold
9                               Bran Chex              Ralston Purina cold
10                            Bran Flakes                        Post cold
11                           Cap'n'Crunch                 Quaker Oats cold
12                               Cheerios               General Mills cold
13                  Cinnamon Toast Crunch               General Mills cold
14                               Clusters               General Mills cold
15                            Cocoa Puffs               General Mills cold
16                              Corn Chex              Ralston Purina cold
17                            Corn Flakes                    Kelloggs cold
18                              Corn Pops                    Kelloggs cold
19                          Count Chocula               General Mills cold
20                     Cracklin' Oat Bran                    Kelloggs cold
21                 Cream of Wheat (Quick)                     Nabisco  hot
22                                Crispix                    Kelloggs cold
23                 Crispy Wheat & Raisins               General Mills cold
24                            Double Chex              Ralston Purina cold
25                            Froot Loops                    Kelloggs cold
26                         Frosted Flakes                    Kelloggs cold
27                    Frosted Mini-Wheats                    Kelloggs cold
28 Fruit & Fibre Dates; Walnuts; and Oats                        Post cold
29                          Fruitful Bran                    Kelloggs cold
30                         Fruity Pebbles                        Post cold
31                           Golden Crisp                        Post cold
32                         Golden Grahams               General Mills cold
33                      Grape Nuts Flakes                        Post cold
34                             Grape-Nuts                        Post cold
35                     Great Grains Pecan                        Post cold
36                       Honey Graham Ohs                 Quaker Oats cold
37                     Honey Nut Cheerios               General Mills cold
38                             Honey-comb                        Post cold
39            Just Right Crunchy  Nuggets                    Kelloggs cold
40                 Just Right Fruit & Nut                    Kelloggs cold
41                                    Kix               General Mills cold
42                                   Life                 Quaker Oats cold
43                           Lucky Charms               General Mills cold
44                                  Maypo American Home Food Products  hot
45       Muesli Raisins; Dates; & Almonds              Ralston Purina cold
46      Muesli Raisins; Peaches; & Pecans              Ralston Purina cold
47                   Mueslix Crispy Blend                    Kelloggs cold
48                   Multi-Grain Cheerios               General Mills cold
49                       Nut&Honey Crunch                    Kelloggs cold
50              Nutri-Grain Almond-Raisin                    Kelloggs cold
51                      Nutri-grain Wheat                    Kelloggs cold
52                   Oatmeal Raisin Crisp               General Mills cold
53                  Post Nat. Raisin Bran                        Post cold
54                             Product 19                    Kelloggs cold
55                            Puffed Rice                 Quaker Oats cold
56                           Puffed Wheat                 Quaker Oats cold
57                     Quaker Oat Squares                 Quaker Oats cold
58                         Quaker Oatmeal                 Quaker Oats  hot
59                            Raisin Bran                    Kelloggs cold
60                        Raisin Nut Bran               General Mills cold
61                         Raisin Squares                    Kelloggs cold
62                              Rice Chex              Ralston Purina cold
63                          Rice Krispies                    Kelloggs cold
64                         Shredded Wheat                     Nabisco cold
65                 Shredded Wheat 'n'Bran                     Nabisco cold
66              Shredded Wheat spoon size                     Nabisco cold
67                                 Smacks                    Kelloggs cold
68                              Special K                    Kelloggs cold
69                Strawberry Fruit Wheats                     Nabisco cold
70                      Total Corn Flakes               General Mills cold
71                      Total Raisin Bran               General Mills cold
72                      Total Whole Grain               General Mills cold
73                                Triples               General Mills cold
74                                   Trix               General Mills cold
75                             Wheat Chex              Ralston Purina cold
76                               Wheaties               General Mills cold
77                    Wheaties Honey Gold               General Mills cold
   calories protein fat sodium fiber carbo sugars potass vitamins shelf weight
1        70       4   1    130  10.0   5.0      6    280       25     3   1.00
2       120       3   5     15   2.0   8.0      8    135        0     3   1.00
3        70       4   1    260   9.0   7.0      5    320       25     3   1.00
4        50       4   0    140  14.0   8.0      0    330       25     3   1.00
5       110       2   2    200   1.0  14.0      8     -1       25     3   1.00
6       110       2   2    180   1.5  10.5     10     70       25     1   1.00
7       110       2   0    125   1.0  11.0     14     30       25     2   1.00
8       130       3   2    210   2.0  18.0      8    100       25     3   1.33
9        90       2   1    200   4.0  15.0      6    125       25     1   1.00
10       90       3   0    210   5.0  13.0      5    190       25     3   1.00
11      120       1   2    220   0.0  12.0     12     35       25     2   1.00
12      110       6   2    290   2.0  17.0      1    105       25     1   1.00
13      120       1   3    210   0.0  13.0      9     45       25     2   1.00
14      110       3   2    140   2.0  13.0      7    105       25     3   1.00
15      110       1   1    180   0.0  12.0     13     55       25     2   1.00
16      110       2   0    280   0.0  22.0      3     25       25     1   1.00
17      100       2   0    290   1.0  21.0      2     35       25     1   1.00
18      110       1   0     90   1.0  13.0     12     20       25     2   1.00
19      110       1   1    180   0.0  12.0     13     65       25     2   1.00
20      110       3   3    140   4.0  10.0      7    160       25     3   1.00
21      100       3   0     80   1.0  21.0      0     -1        0     2   1.00
22      110       2   0    220   1.0  21.0      3     30       25     3   1.00
23      100       2   1    140   2.0  11.0     10    120       25     3   1.00
24      100       2   0    190   1.0  18.0      5     80       25     3   1.00
25      110       2   1    125   1.0  11.0     13     30       25     2   1.00
26      110       1   0    200   1.0  14.0     11     25       25     1   1.00
27      100       3   0      0   3.0  14.0      7    100       25     2   1.00
28      120       3   2    160   5.0  12.0     10    200       25     3   1.25
29      120       3   0    240   5.0  14.0     12    190       25     3   1.33
30      110       1   1    135   0.0  13.0     12     25       25     2   1.00
31      100       2   0     45   0.0  11.0     15     40       25     1   1.00
32      110       1   1    280   0.0  15.0      9     45       25     2   1.00
33      100       3   1    140   3.0  15.0      5     85       25     3   1.00
34      110       3   0    170   3.0  17.0      3     90       25     3   1.00
35      120       3   3     75   3.0  13.0      4    100       25     3   1.00
36      120       1   2    220   1.0  12.0     11     45       25     2   1.00
37      110       3   1    250   1.5  11.5     10     90       25     1   1.00
38      110       1   0    180   0.0  14.0     11     35       25     1   1.00
39      110       2   1    170   1.0  17.0      6     60      100     3   1.00
40      140       3   1    170   2.0  20.0      9     95      100     3   1.30
41      110       2   1    260   0.0  21.0      3     40       25     2   1.00
42      100       4   2    150   2.0  12.0      6     95       25     2   1.00
43      110       2   1    180   0.0  12.0     12     55       25     2   1.00
44      100       4   1      0   0.0  16.0      3     95       25     2   1.00
45      150       4   3     95   3.0  16.0     11    170       25     3   1.00
46      150       4   3    150   3.0  16.0     11    170       25     3   1.00
47      160       3   2    150   3.0  17.0     13    160       25     3   1.50
48      100       2   1    220   2.0  15.0      6     90       25     1   1.00
49      120       2   1    190   0.0  15.0      9     40       25     2   1.00
50      140       3   2    220   3.0  21.0      7    130       25     3   1.33
51       90       3   0    170   3.0  18.0      2     90       25     3   1.00
52      130       3   2    170   1.5  13.5     10    120       25     3   1.25
53      120       3   1    200   6.0  11.0     14    260       25     3   1.33
54      100       3   0    320   1.0  20.0      3     45      100     3   1.00
55       50       1   0      0   0.0  13.0      0     15        0     3   0.50
56       50       2   0      0   1.0  10.0      0     50        0     3   0.50
57      100       4   1    135   2.0  14.0      6    110       25     3   1.00
58      100       5   2      0   2.7  -1.0     -1    110        0     1   1.00
59      120       3   1    210   5.0  14.0     12    240       25     2   1.33
60      100       3   2    140   2.5  10.5      8    140       25     3   1.00
61       90       2   0      0   2.0  15.0      6    110       25     3   1.00
62      110       1   0    240   0.0  23.0      2     30       25     1   1.00
63      110       2   0    290   0.0  22.0      3     35       25     1   1.00
64       80       2   0      0   3.0  16.0      0     95        0     1   0.83
65       90       3   0      0   4.0  19.0      0    140        0     1   1.00
66       90       3   0      0   3.0  20.0      0    120        0     1   1.00
67      110       2   1     70   1.0   9.0     15     40       25     2   1.00
68      110       6   0    230   1.0  16.0      3     55       25     1   1.00
69       90       2   0     15   3.0  15.0      5     90       25     2   1.00
70      110       2   1    200   0.0  21.0      3     35      100     3   1.00
71      140       3   1    190   4.0  15.0     14    230      100     3   1.50
72      100       3   1    200   3.0  16.0      3    110      100     3   1.00
73      110       2   1    250   0.0  21.0      3     60       25     3   1.00
74      110       1   1    140   0.0  13.0     12     25       25     2   1.00
75      100       3   1    230   3.0  17.0      3    115       25     1   1.00
76      100       3   1    200   3.0  17.0      3    110       25     1   1.00
77      110       2   1    200   1.0  16.0      8     60       25     1   1.00
   cups   rating
1  0.33 68.40297
2  1.00 33.98368
3  0.33 59.42551
4  0.50 93.70491
5  0.75 34.38484
6  0.75 29.50954
7  1.00 33.17409
8  0.75 37.03856
9  0.67 49.12025
10 0.67 53.31381
11 0.75 18.04285
12 1.25 50.76500
13 0.75 19.82357
14 0.50 40.40021
15 1.00 22.73645
16 1.00 41.44502
17 1.00 45.86332
18 1.00 35.78279
19 1.00 22.39651
20 0.50 40.44877
21 1.00 64.53382
22 1.00 46.89564
23 0.75 36.17620
24 0.75 44.33086
25 1.00 32.20758
26 0.75 31.43597
27 0.80 58.34514
28 0.67 40.91705
29 0.67 41.01549
30 0.75 28.02576
31 0.88 35.25244
32 0.75 23.80404
33 0.88 52.07690
34 0.25 53.37101
35 0.33 45.81172
36 1.00 21.87129
37 0.75 31.07222
38 1.33 28.74241
39 1.00 36.52368
40 0.75 36.47151
41 1.50 39.24111
42 0.67 45.32807
43 1.00 26.73451
44 1.00 54.85092
45 1.00 37.13686
46 1.00 34.13976
47 0.67 30.31335
48 1.00 40.10596
49 0.67 29.92429
50 0.67 40.69232
51 1.00 59.64284
52 0.50 30.45084
53 0.67 37.84059
54 1.00 41.50354
55 1.00 60.75611
56 1.00 63.00565
57 0.50 49.51187
58 0.67 50.82839
59 0.75 39.25920
60 0.50 39.70340
61 0.50 55.33314
62 1.13 41.99893
63 1.00 40.56016
64 1.00 68.23588
65 0.67 74.47295
66 0.67 72.80179
67 0.75 31.23005
68 1.00 53.13132
69 1.00 59.36399
70 1.00 38.83975
71 1.00 28.59278
72 1.00 46.65884
73 0.75 39.10617
74 1.00 27.75330
75 0.67 49.78744
76 1.00 51.59219
77 0.75 36.18756

group_by() + slice()

cereal |> 
  group_by(manuf) |> 
  slice_max(order_by = calories)
# A tibble: 12 × 16
# Groups:   manuf [7]
   name      manuf type  calories protein   fat sodium fiber carbo sugars potass
   <fct>     <fct> <fct>    <int>   <int> <int>  <int> <dbl> <dbl>  <int>  <int>
 1 Maypo     A     hot        100       4     1      0     0    16      3     95
 2 Total Ra… G     cold       140       3     1    190     4    15     14    230
 3 Mueslix … K     cold       160       3     2    150     3    17     13    160
 4 Cream of… N     hot        100       3     0     80     1    21      0     -1
 5 Fruit & … P     cold       120       3     2    160     5    12     10    200
 6 Great Gr… P     cold       120       3     3     75     3    13      4    100
 7 Post Nat… P     cold       120       3     1    200     6    11     14    260
 8 100% Nat… Q     cold       120       3     5     15     2     8      8    135
 9 Cap'n'Cr… Q     cold       120       1     2    220     0    12     12     35
10 Honey Gr… Q     cold       120       1     2    220     1    12     11     45
11 Muesli R… R     cold       150       4     3     95     3    16     11    170
12 Muesli R… R     cold       150       4     3    150     3    16     11    170
# ℹ 5 more variables: vitamins <int>, shelf <int>, weight <dbl>, cups <dbl>,
#   rating <dbl>

Multiple Variables in slice()

cereal |> 
  arrange(calories, sugars) |> 
  slice(1:3)
                       name manuf type calories protein fat sodium fiber carbo
1 All-Bran with Extra Fiber     K cold       50       4   0    140    14     8
2               Puffed Rice     Q cold       50       1   0      0     0    13
3              Puffed Wheat     Q cold       50       2   0      0     1    10
  sugars potass vitamins shelf weight cups   rating
1      0    330       25     3    1.0  0.5 93.70491
2      0     15        0     3    0.5  1.0 60.75611
3      0     50        0     3    0.5  1.0 63.00565

Piping into ggplot()

cereal |> 
  mutate(manuf = case_when(manuf == "A" ~ "American Home Food Products", 
                           manuf == "G" ~ "General Mills", 
                           manuf == "K" ~ "Kelloggs", 
                           manuf == "N" ~ "Nabisco", 
                           manuf == "P" ~ "Post", 
                           manuf == "Q" ~ "Quaker Oats", 
                           manuf == "R" ~ "Ralston Purina"
                           )
  ) |> 
  filter(type == "cold") |> 
  mutate(cal_per_cup = calories / cups) |> 
  group_by(manuf) |> 
  summarise(mean_cal_per_cup = mean(cal_per_cup)) |> 
  ggplot(aes(x = manuf, 
             y = mean_cal_per_cup, 
             shape = manuf)) +
  geom_point(show.legend = FALSE,
             size = 3) +
  labs(x = "Manufacturer",
       subtitle = "Calories per cup (mean)") +
  theme_bw() +
  theme(axis.title.y = element_blank(),
        axis.title.x  = element_text(size = 32),
        plot.subtitle = element_text(size = 32),
        axis.text = element_text(size = 28)
        )

Piping into ggplot()

Implication of Data Ethics

Data Science Oath

I will not be ashamed to say, “I know not,” nor will I fail to call in my colleagues when the skills of another are needed for solving a problem.​

I will respect the privacy of my data subjects, for their data are not disclosed to me that the world may know, so I will tread with care in matters of privacy and security.

I will remember that my data are not just numbers without meaning or context, but represent real people and situations, and that my work may lead to unintended societal consequences, such as inequality, poverty, and disparities due to algorithmic bias.

ASA Ethical Guidelines

The American Statistical Association’s Ethical Guidelines for Statistical Practice are intended to help statistics practitioners make decisions ethically. Additionally, the ethical guidelines aim to promote accountability by informing those who rely on statistical analysis of the standards they should expect.

Visit Ethical Guidelines for Statistical Practice and discuss one of the guidelines with your partner.

  • What surprises you? What did you learn?
  • What scenario might this come into play?

Institutional Review Board

IRB reviews help to ensure that research participants are protected from research-related risks and treated ethically, a necessary prerequisite for maintaining the public’s trust in the research enterprise and allowing science to advance for the common good.

Note

Watch a video about IRB to learn more.

Weekly Lab + Challenge

To do…

  • Lab 3: Hip Hop Lyrics
    • Due Friday, 1/27 at 11:59pm
  • Challenge 3: Published Comparisons & Data Ethics
    • Due Saturday, 1/28 at 11:59pm
  • Read Chapter 4: Data Joins and Transformations
    • Concept Check 4.1 + 4.2 due Monday (1/30) at 8:00am